home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / sprite / cccp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-26  |  142.9 KB  |  5,647 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2. Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.                     Written by Paul Rubin, June 1986
  4.             Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 1, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. typedef unsigned char U_CHAR;
  25.  
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <signal.h>
  31. #include <alloca.h>
  32.  
  33. #ifndef VMS
  34. #include <sys/file.h>
  35. #ifndef USG
  36. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  37. #include <sys/resource.h>
  38. #else
  39. #define index strchr
  40. #define rindex strrchr
  41. #include <time.h>
  42. #include <fcntl.h>
  43. #endif /* USG */
  44. #endif /* not VMS */
  45.   
  46. /* VMS-specific definitions */
  47. #ifdef VMS
  48. #include <time.h>
  49. #include <errno.h>        /* This defines "errno" properly */
  50. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  51. #define O_RDONLY    0    /* Open arg for Read/Only  */
  52. #define O_WRONLY    1    /* Open arg for Write/Only */
  53. #define read(fd,buf,size)    VAX11_C_read(fd,buf,size)
  54. #define write(fd,buf,size)    VAX11_C_write(fd,buf,size)
  55. #ifdef __GNUC__
  56. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  57. #endif /* __GNUC__ */
  58. #endif /* VMS */
  59.  
  60. #define max(a,b) ((a) > (b) ? (a) : (b))
  61.  
  62. /* External declarations.  */
  63.  
  64. void bcopy (), bzero ();
  65. int bcmp ();
  66. extern char *getenv ();
  67. extern char *version_string;
  68.  
  69. /* Forward declarations.  */
  70.  
  71. struct directive;
  72. struct file_buf;
  73. struct arglist;
  74. struct argdata;
  75.  
  76. int do_define (), do_line (), do_include (), do_undef (), do_error (),
  77.   do_pragma (), do_if (), do_xifdef (), do_else (),
  78.   do_elif (), do_endif (), do_sccs (), do_once ();
  79.  
  80. struct hashnode *install ();
  81. struct hashnode *lookup ();
  82.  
  83. char *xmalloc (), *xrealloc (), *xcalloc (), *savestring ();
  84. void fatal (), fancy_abort (), pfatal_with_name (), perror_with_name ();
  85.  
  86. void macroexpand ();
  87. void dump_all_macros ();
  88. void conditional_skip ();
  89. void skip_if_group ();
  90. void output_line_command ();
  91. /* Last arg to output_line_command.  */
  92. enum file_change_code {same_file, enter_file, leave_file};
  93.  
  94. int grow_outbuf ();
  95. int handle_directive ();
  96. void memory_full ();
  97.  
  98. U_CHAR *macarg1 ();
  99. char *macarg ();
  100.  
  101. U_CHAR *skip_to_end_of_comment ();
  102. U_CHAR *skip_quoted_string ();
  103.  
  104. #ifndef FATAL_EXIT_CODE
  105. #define FATAL_EXIT_CODE 33    /* gnu cc command understands this */
  106. #endif
  107.  
  108. #ifndef SUCCESS_EXIT_CODE
  109. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  110. #endif
  111.  
  112. /* Name under which this program was invoked.  */
  113.  
  114. char *progname;
  115.  
  116. /* Nonzero means handle C++ comment syntax and use
  117.    extra default include directories for C++.  */
  118.  
  119. int cplusplus;
  120.  
  121. /* Current maximum length of directory names in the search path
  122.    for include files.  (Altered as we get more of them.)  */
  123.  
  124. int max_include_len;
  125.  
  126. /* Nonzero means copy comments into the output file.  */
  127.  
  128. int put_out_comments = 0;
  129.  
  130. /* Nonzero means don't process the ANSI trigraph sequences.  */
  131.  
  132. int no_trigraphs = 0;
  133.  
  134. /* Nonzero means print the names of included files rather than
  135.    the preprocessed output.  1 means just the #include "...",
  136.    2 means #include <...> as well.  */
  137.  
  138. int print_deps = 0;
  139.  
  140. /* Nonzero means don't output line number information.  */
  141.  
  142. int no_line_commands;
  143.  
  144. /* Nonzero means inhibit output of the preprocessed text
  145.    and instead output the definitions of all user-defined macros
  146.    in a form suitable for use as input to cccp.  */
  147.  
  148. int dump_macros;
  149.  
  150. /* Nonzero means give all the error messages the ANSI standard requires.  */
  151.  
  152. int pedantic;
  153.  
  154. /* Nonzero means warn if slash-star appears in a comment.  */
  155.  
  156. int warn_comments;
  157.  
  158. /* Nonzero means warn if there are any trigraphs.  */
  159.  
  160. int warn_trigraphs;
  161.  
  162. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  163.  
  164. int traditional;
  165.  
  166. /* Nonzero causes output not to be done,
  167.    but directives such as #define that have side effects
  168.    are still obeyed.  */
  169.  
  170. int no_output;
  171.  
  172. /* I/O buffer structure.
  173.    The `fname' field is nonzero for source files and #include files
  174.    and for the dummy text used for -D and -U.
  175.    It is zero for rescanning results of macro expansion
  176.    and for expanding macro arguments.  */
  177. #define INPUT_STACK_MAX 200
  178. struct file_buf {
  179.   char *fname;
  180.   int lineno;
  181.   int length;
  182.   U_CHAR *buf;
  183.   U_CHAR *bufp;
  184.   /* Macro that this level is the expansion of.
  185.      Included so that we can reenable the macro
  186.      at the end of this level.  */
  187.   struct hashnode *macro;
  188.   /* Value of if_stack at start of this file.
  189.      Used to prohibit unmatched #endif (etc) in an include file.  */
  190.   struct if_stack *if_stack;
  191.   /* Object to be freed at end of input at this level.  */
  192.   U_CHAR *free_ptr;
  193. } instack[INPUT_STACK_MAX];
  194.  
  195. /* Current nesting level of input sources.
  196.    `instack[indepth]' is the level currently being read.  */
  197. int indepth = -1;
  198. #define CHECK_DEPTH(code) \
  199.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  200.     {                                    \
  201.       error_with_line (line_for_error (instack[indepth].lineno),    \
  202.                "macro or #include recursion too deep");        \
  203.       code;                                \
  204.     }
  205.  
  206. /* Current depth in #include directives that use <...>.  */
  207. int system_include_depth = 0;
  208.  
  209. typedef struct file_buf FILE_BUF;
  210.  
  211. /* The output buffer.  Its LENGTH field is the amount of room allocated
  212.    for the buffer, not the number of chars actually present.  To get
  213.    that, subtract outbuf.buf from outbuf.bufp. */
  214.  
  215. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  216. FILE_BUF outbuf;
  217.  
  218. /* Grow output buffer OBUF points at
  219.    so it can hold at least NEEDED more chars.  */
  220.  
  221. #define check_expand(OBUF, NEEDED)  \
  222.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  223.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  224.  
  225. struct file_name_list
  226.   {
  227.     struct file_name_list *next;
  228.     char *fname;
  229.   };
  230.  
  231. /* #include "file" looks in source file dir, then stack. */
  232. /* #include <file> just looks in the stack. */
  233. /* -I directories are added to the end, then the defaults are added. */
  234.  
  235. /*
  236.  * Use different default include directories for Sprite.  The second
  237.  * default directory gets filled in once the machine type is known.
  238.  */
  239.  
  240. #define GCC_INCLUDE_DIR "/sprite/lib/include"
  241. #define GPLUSPLUS_INCLUDE_DIR "/sprite/lib/include"
  242.  
  243. struct file_name_list include_defaults[] =
  244.   {
  245. #ifdef sprite
  246.     { 0, 0 },
  247.     { 0, GCC_INCLUDE_DIR },
  248. #else       
  249. #ifndef VMS
  250.     { &include_defaults[1], GCC_INCLUDE_DIR },
  251.     { &include_defaults[2], "/usr/include" },
  252.     { 0, "/usr/local/include" }
  253. #else
  254.     { &include_defaults[1], "GNU_CC_INCLUDE:" },       /* GNU includes */
  255.     { &include_defaults[2], "SYS$SYSROOT:[SYSLIB.]" }, /* VAX-11 "C" includes */
  256.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  257. #endif /* VMS */
  258. #endif /* sprite */
  259.   };
  260.  
  261. /* These are used instead of the above, for C++.  */
  262. struct file_name_list cplusplus_include_defaults[] =
  263.   {
  264. #ifdef sprite
  265.     { 0, 0 },
  266.     { 0, GPLUSPLUS_INCLUDE_DIR },
  267. #else
  268. #ifndef VMS
  269.     /* Pick up GNU C++ specific include files.  */
  270.     { &cplusplus_include_defaults[1], GPLUSPLUS_INCLUDE_DIR },
  271.     /* Use GNU CC specific header files.  */
  272.     { &cplusplus_include_defaults[2], GCC_INCLUDE_DIR },
  273.     { 0, "/usr/include" }
  274. #else
  275.     { &cplusplus_include_defaults[1], "GNU_GXX_INCLUDE:" },
  276.     { &cplusplus_include_defaults[2], "GNU_CC_INCLUDE:" },
  277.     /* VAX-11 C includes */
  278.     { &cplusplus_include_defaults[3], "SYS$SYSROOT:[SYSLIB.]" },
  279.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  280. #endif /* VMS */
  281. #endif /* sprite */
  282.   };
  283.  
  284. struct file_name_list *include = 0;    /* First dir to search */
  285.     /* First dir to search for <file> */
  286. struct file_name_list *first_bracket_include = 0;
  287. struct file_name_list *last_include = 0;    /* Last in chain */
  288.  
  289. /* List of included files that contained #once.  */
  290. struct file_name_list *dont_repeat_files = 0;
  291.  
  292. /* List of other included files.  */
  293. struct file_name_list *all_include_files = 0;
  294.  
  295. /* Structure allocated for every #define.  For a simple replacement
  296.    such as
  297.        #define foo bar ,
  298.    nargs = -1, the `pattern' list is null, and the expansion is just
  299.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  300.    e.g.,
  301.        #define getchar() getc (stdin) .
  302.    When there are args, the expansion is the replacement text with the
  303.    args squashed out, and the reflist is a list describing how to
  304.    build the output from the input: e.g., "3 chars, then the 1st arg,
  305.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  306.    The chars here come from the expansion.  Whatever is left of the
  307.    expansion after the last arg-occurrence is copied after that arg.
  308.    Note that the reflist can be arbitrarily long---
  309.    its length depends on the number of times the arguments appear in
  310.    the replacement text, not how many args there are.  Example:
  311.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  312.    pattern list
  313.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  314.    where (x, y) means (nchars, argno). */
  315.  
  316. typedef struct definition DEFINITION;
  317. struct definition {
  318.   int nargs;
  319.   int length;            /* length of expansion string */
  320.   U_CHAR *expansion;
  321.   struct reflist {
  322.     struct reflist *next;
  323.     char stringify;        /* nonzero if this arg was preceded by a
  324.                    # operator. */
  325.     char raw_before;        /* Nonzero if a ## operator before arg. */
  326.     char raw_after;        /* Nonzero if a ## operator after arg. */
  327.     int nchars;            /* Number of literal chars to copy before
  328.                    this arg occurrence.  */
  329.     int argno;            /* Number of arg to substitute (origin-0) */
  330.   } *pattern;
  331.   /* Names of macro args, concatenated in reverse order
  332.      with comma-space between them.
  333.      The only use of this is that we warn on redefinition
  334.      if this differs between the old and new definitions.  */
  335.   U_CHAR *argnames;
  336. };
  337.  
  338. /* different kinds of things that can appear in the value field
  339.    of a hash node.  Actually, this may be useless now. */
  340. union hashval {
  341.   int ival;
  342.   char *cpval;
  343.   DEFINITION *defn;
  344. };
  345.  
  346.  
  347. /* The structure of a node in the hash table.  The hash table
  348.    has entries for all tokens defined by #define commands (type T_MACRO),
  349.    plus some special tokens like __LINE__ (these each have their own
  350.    type, and the appropriate code is run when that type of node is seen.
  351.    It does not contain control words like "#define", which are recognized
  352.    by a separate piece of code. */
  353.  
  354. /* different flavors of hash nodes --- also used in keyword table */
  355. enum node_type {
  356.  T_DEFINE = 1,    /* the `#define' keyword */
  357.  T_INCLUDE,    /* the `#include' keyword */
  358.  T_IFDEF,    /* the `#ifdef' keyword */
  359.  T_IFNDEF,    /* the `#ifndef' keyword */
  360.  T_IF,        /* the `#if' keyword */
  361.  T_ELSE,    /* `#else' */
  362.  T_PRAGMA,    /* `#pragma' */
  363.  T_ELIF,    /* `#else' */
  364.  T_UNDEF,    /* `#undef' */
  365.  T_LINE,    /* `#line' */
  366.  T_ERROR,    /* `#error' */
  367.  T_ENDIF,    /* `#endif' */
  368.  T_SCCS,    /* `#sccs', used on system V.  */
  369.  T_IDENT,    /* `#ident', used on system V.  */
  370.  T_SPECLINE,    /* special symbol `__LINE__' */
  371.  T_DATE,    /* `__DATE__' */
  372.  T_FILE,    /* `__FILE__' */
  373.  T_BASE_FILE,    /* `__BASE_FILE__' */
  374.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  375.  T_VERSION,    /* `__VERSION__' */
  376.  T_TIME,    /* `__TIME__' */
  377.  T_CONST,    /* Constant value, used by `__STDC__' */
  378.  T_MACRO,    /* macro defined by `#define' */
  379.  T_DISABLED,    /* macro temporarily turned off for rescan */
  380.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  381.  T_UNUSED    /* Used for something not defined.  */
  382.  };
  383.  
  384. struct hashnode {
  385.   struct hashnode *next;    /* double links for easy deletion */
  386.   struct hashnode *prev;
  387.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  388.                    chain is kept, in case the node is the head
  389.                    of the chain and gets deleted. */
  390.   enum node_type type;        /* type of special token */
  391.   int length;            /* length of token, for quick comparison */
  392.   U_CHAR *name;            /* the actual name */
  393.   union hashval value;        /* pointer to expansion, or whatever */
  394. };
  395.  
  396. typedef struct hashnode HASHNODE;
  397.  
  398. /* Some definitions for the hash table.  The hash function MUST be
  399.    computed as shown in hashf () below.  That is because the rescan
  400.    loop computes the hash value `on the fly' for most tokens,
  401.    in order to avoid the overhead of a lot of procedure calls to
  402.    the hashf () function.  Hashf () only exists for the sake of
  403.    politeness, for use when speed isn't so important. */
  404.  
  405. #define HASHSIZE 1403
  406. HASHNODE *hashtab[HASHSIZE];
  407. #define HASHSTEP(old, c) ((old << 2) + c)
  408. #define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
  409.  
  410. /* Symbols to predefine.  */
  411.  
  412. #ifdef CPP_PREDEFINES
  413. char *predefs = CPP_PREDEFINES;
  414. #else
  415. char *predefs = "";
  416. #endif
  417.  
  418. /* `struct directive' defines one #-directive, including how to handle it.  */
  419.  
  420. struct directive {
  421.   int length;            /* Length of name */
  422.   int (*func)();        /* Function to handle directive */
  423.   char *name;            /* Name of directive */
  424.   enum node_type type;        /* Code which describes which directive. */
  425.   char angle_brackets;        /* Nonzero => <...> is special.  */
  426.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  427.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  428. };
  429.  
  430. /* Here is the actual list of #-directives, most-often-used first.  */
  431.  
  432. struct directive directive_table[] = {
  433.   {  6, do_define, "define", T_DEFINE, 0, 1},
  434.   {  2, do_if, "if", T_IF},
  435.   {  5, do_xifdef, "ifdef", T_IFDEF},
  436.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  437.   {  5, do_endif, "endif", T_ENDIF},
  438.   {  4, do_else, "else", T_ELSE},
  439.   {  4, do_elif, "elif", T_ELIF},
  440.   {  4, do_line, "line", T_LINE},
  441.   {  7, do_include, "include", T_INCLUDE, 1},
  442.   {  5, do_undef, "undef", T_UNDEF},
  443.   {  5, do_error, "error", T_ERROR},
  444. #ifdef SCCS_DIRECTIVE
  445.   {  4, do_sccs, "sccs", T_SCCS},
  446. #endif
  447.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  448.   {  -1, 0, "", T_UNUSED},
  449. };
  450.  
  451. /* table to tell if char can be part of a C identifier. */
  452. U_CHAR is_idchar[256];
  453. /* table to tell if char can be first char of a c identifier. */
  454. U_CHAR is_idstart[256];
  455. /* table to tell if c is horizontal space.  */
  456. U_CHAR is_hor_space[256];
  457. /* table to tell if c is horizontal or vertical space.  */
  458. U_CHAR is_space[256];
  459.  
  460. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  461. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  462.   
  463. int errors = 0;            /* Error counter for exit code */
  464.  
  465. /* Zero means dollar signs are punctuation.
  466.    -$ stores 0; -traditional, stores 1.  Default is 1 for VMS, 0 otherwise.
  467.    This must be 0 for correct processing of this ANSI C program:
  468.     #define foo(a) #a
  469.     #define lose(b) foo(b)
  470.     #define test$
  471.     lose(test)    */
  472. #ifndef DOLLARS_IN_IDENTIFIERS
  473. #define DOLLARS_IN_IDENTIFIERS 0
  474. #endif
  475. int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  476.  
  477. FILE_BUF expand_to_temp_buffer ();
  478.  
  479. DEFINITION *collect_expansion ();
  480.  
  481. /* Stack of conditionals currently in progress
  482.    (including both successful and failing conditionals).  */
  483.  
  484. struct if_stack {
  485.   struct if_stack *next;    /* for chaining to the next stack frame */
  486.   char *fname;        /* copied from input when frame is made */
  487.   int lineno;            /* similarly */
  488.   int if_succeeded;        /* true if a leg of this if-group
  489.                     has been passed through rescan */
  490.   enum node_type type;        /* type of last directive seen in this group */
  491. };
  492. typedef struct if_stack IF_STACK_FRAME;
  493. IF_STACK_FRAME *if_stack = NULL;
  494.  
  495. /* Buffer of -M output.  */
  496.  
  497. char *deps_buffer;
  498.  
  499. /* Number of bytes allocated in above.  */
  500. int deps_allocated_size;
  501.  
  502. /* Number of bytes used.  */
  503. int deps_size;
  504.  
  505. /* Number of bytes since the last newline.  */
  506. int deps_column;
  507.  
  508. /* Nonzero means -I- has been seen,
  509.    so don't look for #include "foo" the source-file directory.  */
  510. int ignore_srcdir;
  511.  
  512. /* Handler for SIGPIPE.  */
  513.  
  514. static void
  515. pipe_closed ()
  516. {
  517.   fatal ("output pipe has been closed");
  518. }
  519.  
  520. int
  521. main (argc, argv)
  522.      int argc;
  523.      char **argv;
  524. {
  525.   int st_mode;
  526.   long st_size;
  527.   char *in_fname, *out_fname;
  528.   int f, i;
  529.   FILE_BUF *fp;
  530.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  531.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  532.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  533.   int inhibit_predefs = 0;
  534.   int no_standard_includes = 0;
  535.   char *machine = NULL;
  536.  
  537.   /* Non-0 means don't output the preprocessed program.  */
  538.   int inhibit_output = 0;
  539.  
  540.   /* Stream on which to print the dependency information.  */
  541.   FILE *deps_stream = 0;
  542.   /* Target-name to write with the dependency information.  */
  543.   char *deps_target = 0;
  544.  
  545. #ifdef RLIMIT_STACK
  546.   /* Get rid of any avoidable limit on stack size.  */
  547.   {
  548.     struct rlimit rlim;
  549.  
  550.     /* Set the stack limit huge so that alloca (particularly stringtab
  551.      * in dbxread.c) does not fail. */
  552.     getrlimit (RLIMIT_STACK, &rlim);
  553.     rlim.rlim_cur = rlim.rlim_max;
  554.     setrlimit (RLIMIT_STACK, &rlim);
  555.   }
  556. #endif /* RLIMIT_STACK defined */
  557.  
  558.   progname = argv[0];
  559. #ifdef VMS
  560.   {
  561.     /* Remove directories from PROGNAME.  */
  562.     char *s;
  563.     extern char *rindex ();
  564.  
  565.     progname = savestring (argv[0]);
  566.  
  567.     if (!(s = rindex (progname, ']')))
  568.       s = rindex (progname, ':');
  569.     if (s)
  570.       strcpy (progname, s+1);
  571.     if (s = rindex (progname, '.'))
  572.       *s = '\0';
  573.   }
  574. #endif
  575.  
  576.   in_fname = NULL;
  577.   out_fname = NULL;
  578.  
  579.   /* Initialize is_idchar to allow $.  */
  580.   dollars_in_ident = 1;
  581.   initialize_char_syntax ();
  582.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  583.  
  584.   no_line_commands = 0;
  585.   no_trigraphs = 1;
  586.   dump_macros = 0;
  587.   no_output = 0;
  588.   cplusplus = 0;
  589. #ifdef CPLUSPLUS
  590.   cplusplus = 1;
  591. #endif
  592.  
  593.   signal (SIGPIPE, pipe_closed);
  594.  
  595. #ifndef VMS
  596.   max_include_len
  597.     = max (max (sizeof (GCC_INCLUDE_DIR),
  598.         sizeof (GPLUSPLUS_INCLUDE_DIR)),
  599.        sizeof ("/usr/include/CC"));
  600. #else /* VMS */
  601.   max_include_len
  602.     = sizeof("SYS$SYSROOT:[SYSLIB.]");
  603. #endif /* VMS */
  604.  
  605.   bzero (pend_files, argc * sizeof (char *));
  606.   bzero (pend_defs, argc * sizeof (char *));
  607.   bzero (pend_undefs, argc * sizeof (char *));
  608.  
  609.   /* Process switches and find input file name.  */
  610.  
  611.   for (i = 1; i < argc; i++) {
  612.     if (argv[i][0] != '-') {
  613.       if (out_fname != NULL)
  614.     fatal ("Usage: %s [switches] input output", argv[0]);
  615.       else if (in_fname != NULL)
  616.     out_fname = argv[i];
  617.       else
  618.     in_fname = argv[i];
  619.     } else {
  620.       switch (argv[i][1]) {
  621.  
  622.       case 'i':
  623.     if (argv[i][2] != 0)
  624.       pend_files[i] = argv[i] + 2;
  625.     else if (i + 1 == argc)
  626.       fatal ("Filename missing after -i option");
  627.     else
  628.       pend_files[i] = argv[i+1], i++;
  629.     break;
  630.  
  631.       case 'o':
  632.     if (out_fname != NULL)
  633.       fatal ("Output filename specified twice");
  634.     if (i + 1 == argc)
  635.       fatal ("Filename missing after -o option");
  636.     out_fname = argv[++i];
  637.     if (!strcmp (out_fname, "-"))
  638.       out_fname = "";
  639.     break;
  640.  
  641.       case 'p':
  642.     pedantic = 1;
  643.     break;
  644.  
  645.       case 't':
  646.     if (!strcmp (argv[i], "-traditional")) {
  647.       traditional = 1;
  648.       dollars_in_ident = 1;
  649.     } else if (!strcmp (argv[i], "-trigraphs")) {
  650.       no_trigraphs = 0;
  651.     }
  652.     break;
  653.  
  654.       case '+':
  655.     cplusplus = 1;
  656.     break;
  657.  
  658.       case 'W':
  659.     if (!strcmp (argv[i], "-Wtrigraphs")) {
  660.       warn_trigraphs = 1;
  661.     }
  662.     if (!strcmp (argv[i], "-Wcomments"))
  663.       warn_comments = 1;
  664.     if (!strcmp (argv[i], "-Wcomment"))
  665.       warn_comments = 1;
  666.     if (!strcmp (argv[i], "-Wall")) {
  667.       warn_trigraphs = 1;
  668.       warn_comments = 1;
  669.     }
  670.     break;
  671.  
  672.       case 'M':
  673.     if (!strcmp (argv[i], "-M"))
  674.       print_deps = 2;
  675.     else if (!strcmp (argv[i], "-MM"))
  676.       print_deps = 1;
  677.     inhibit_output = 1;
  678.     break;
  679.  
  680.       case 'd':
  681.     dump_macros = 1;
  682.     no_output = 1;
  683.     break;
  684.  
  685.       case 'v':
  686.     fprintf (stderr, "GNU CPP version %s\n", version_string);
  687.     break;
  688.  
  689.       case 'D':
  690.     {
  691.       char *p, *p1;
  692.  
  693.       if (argv[i][2] != 0)
  694.         p = argv[i] + 2;
  695.       else if (i + 1 == argc)
  696.         fatal ("Macro name missing after -D option");
  697.       else
  698.         p = argv[++i];
  699.  
  700.       if ((p1 = (char *) index (p, '=')) != NULL)
  701.         *p1 = ' ';
  702.       pend_defs[i] = p;
  703.     }
  704.     break;
  705.  
  706.       case 'U':        /* JF #undef something */
  707.     if (argv[i][2] != 0)
  708.       pend_undefs[i] = argv[i] + 2;
  709.     else if (i + 1 == argc)
  710.       fatal ("Macro name missing after -U option");
  711.     else
  712.       pend_undefs[i] = argv[i+1], i++;
  713.     break;
  714.  
  715.       case 'C':
  716.     put_out_comments = 1;
  717.     break;
  718.  
  719.       case 'E':            /* -E comes from cc -E; ignore it.  */
  720.     break;
  721.  
  722.       case 'P':
  723.     no_line_commands = 1;
  724.     break;
  725.  
  726.       case '$':            /* Don't include $ in identifiers.  */
  727.     dollars_in_ident = 0;
  728.     break;
  729.  
  730.       case 'I':            /* Add directory to path for includes.  */
  731.     {
  732.       struct file_name_list *dirtmp;
  733.  
  734.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
  735.         ignore_srcdir = 1;
  736.       else {
  737.         dirtmp = (struct file_name_list *)
  738.           xmalloc (sizeof (struct file_name_list));
  739.         dirtmp->next = 0;        /* New one goes on the end */
  740.         if (include == 0)
  741.           include = dirtmp;
  742.         else
  743.           last_include->next = dirtmp;
  744.         last_include = dirtmp;    /* Tail follows the last one */
  745.         if (argv[i][2] != 0)
  746.           dirtmp->fname = argv[i] + 2;
  747.         else if (i + 1 == argc)
  748.           fatal ("Directory name missing after -I option");
  749.         else
  750.           dirtmp->fname = argv[++i];
  751.         if (strlen (dirtmp->fname) > max_include_len)
  752.           max_include_len = strlen (dirtmp->fname);
  753.         if (ignore_srcdir && first_bracket_include == 0)
  754.           first_bracket_include = dirtmp;
  755.         }
  756.     }
  757.     break;
  758.  
  759.       case 'n':
  760.     /* -nostdinc causes no default include directories.
  761.        You must specify all include-file directories with -I.  */
  762.     no_standard_includes = 1;
  763.     break;
  764.  
  765.       case 'u':
  766.     /* Sun compiler passes undocumented switch "-undef".
  767.        Let's assume it means to inhibit the predefined symbols.  */
  768.     inhibit_predefs = 1;
  769.     break;
  770.  
  771.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  772.     if (in_fname == NULL) {
  773.       in_fname = "";
  774.       break;
  775.     } else if (out_fname == NULL) {
  776.       out_fname = "";
  777.       break;
  778.     }    /* else fall through into error */
  779.  
  780.       case 'm':
  781.     if (argv[i][2] != 0)
  782.       machine = argv[i] + 2;
  783.     else
  784.       machine = argv[i+1], i++;
  785.     break;
  786.  
  787.       default:
  788.     fatal ("Invalid option `%s'", argv[i]);
  789.       }
  790.     }
  791.   }
  792.  
  793. #ifdef sprite
  794.   /* Use machine information to complete the initialization of the
  795.   directory stack. */
  796.   if (machine == NULL) {
  797.     machine = getenv("MACHINE");
  798.   }
  799.   if (machine != NULL) {
  800.     include_defaults[0].fname = xmalloc((unsigned) (strlen(machine) + 30));
  801.     sprintf(include_defaults[0].fname, "/sprite/lib/include/%s.md", machine);
  802.     include_defaults[0].next = &include_defaults[1];
  803.     cplusplus_include_defaults[0].fname = include_defaults[0].fname;
  804.     cplusplus_include_defaults[0].next = &cplusplus_include_defaults[1];
  805.   } else {
  806.     include_defaults[0].fname = include_defaults[1].fname;
  807.     include_defaults[1].fname = NULL;
  808.     cplusplus_include_defaults[0].fname = cplusplus_include_defaults[1].fname;
  809.     cplusplus_include_defaults[1].fname = NULL;
  810.   }
  811.   for (i=0;i<sizeof(include_defaults)/sizeof(struct file_name_list);i++) {
  812.       if (include_defaults[i].fname == NULL) continue;
  813.       if (strlen(include_defaults[i].fname)>max_include_len) {
  814.       max_include_len = strlen(include_defaults[i].fname);
  815.       }
  816.   }
  817. #endif /* sprite */
  818.  
  819.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  820.   initialize_char_syntax ();
  821.  
  822.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  823.      and option processing.  */
  824.   initialize_builtins ();
  825.  
  826.   /* Do standard #defines that identify processor type.  */
  827.  
  828.   if (!inhibit_predefs) {
  829.     char *p = (char *) alloca (strlen (predefs) + 1);
  830.     strcpy (p, predefs);
  831.     while (*p) {
  832.       char *q;
  833.       if (p[0] != '-' || p[1] != 'D')
  834.     abort ();
  835.       q = &p[2];
  836.       while (*p && *p != ' ') p++;
  837.       if (*p != 0)
  838.     *p++= 0;
  839.       make_definition (q);
  840.     }
  841.   }
  842.  
  843.   /* Do defines specified with -D.  */
  844.   for (i = 1; i < argc; i++)
  845.     if (pend_defs[i])
  846.       make_definition (pend_defs[i]);
  847.  
  848.   /* Do undefines specified with -U.  */
  849.   for (i = 1; i < argc; i++)
  850.     if (pend_undefs[i])
  851.       make_undef (pend_undefs[i]);
  852.  
  853.   /* Unless -fnostdinc,
  854.      tack on the standard include file dirs to the specified list */
  855.   if (!no_standard_includes) {
  856.     if (include == 0)
  857.       include = (cplusplus ? cplusplus_include_defaults : include_defaults);
  858.     else
  859.       last_include->next
  860.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  861.     /* Make sure the list for #include <...> also has the standard dirs.  */
  862.     if (ignore_srcdir && first_bracket_include == 0)
  863.       first_bracket_include
  864.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  865.   }
  866.  
  867.   /* Initialize output buffer */
  868.  
  869.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  870.   outbuf.bufp = outbuf.buf;
  871.   outbuf.length = OUTBUF_SIZE;
  872.  
  873.   /* Scan the -i files before the main input.
  874.      Much like #including them, but with no_output set
  875.      so that only their macro definitions matter.  */
  876.  
  877.   no_output++;
  878.   for (i = 1; i < argc; i++)
  879.     if (pend_files[i]) {
  880.       int fd = open (pend_files[i], O_RDONLY, 0666);
  881.       if (fd < 0) {
  882.     perror_with_name (pend_files[i]);
  883.     return FATAL_EXIT_CODE;
  884.       }
  885.       finclude (fd, pend_files[i], &outbuf);
  886.     }
  887.   no_output--;
  888.  
  889.   /* Create an input stack level for the main input file
  890.      and copy the entire contents of the file into it.  */
  891.  
  892.   fp = &instack[++indepth];
  893.  
  894.   /* JF check for stdin */
  895.   if (in_fname == NULL || *in_fname == 0) {
  896.     in_fname = "";
  897.     f = 0;
  898.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  899.     goto perror;
  900.  
  901.   /* Either of two environment variables can specify output of deps.
  902.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  903.      where OUTPUT_FILE is the file to write deps info to
  904.      and DEPS_TARGET is the target to mention in the deps.  */
  905.  
  906.   if (print_deps == 0
  907.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  908.       || getenv ("DEPENDENCIES_OUTPUT") != 0))
  909.     {
  910.       char *spec = getenv ("DEPENDENCIES_OUTPUT");
  911.       char *s;
  912.       char *output_file;
  913.  
  914.       if (spec == 0)
  915.     {
  916.       spec = getenv ("SUNPRO_DEPENDENCIES");
  917.       print_deps = 2;
  918.     }
  919.       else
  920.     print_deps = 1;
  921.  
  922.       s = spec;
  923.       /* Find the space before the DEPS_TARGET, if there is one.  */
  924.       /* Don't use `index'; that causes trouble on USG.  */
  925.       while (*s != 0 && *s != ' ') s++;
  926.       if (*s != 0)
  927.     {
  928.       deps_target = s + 1;
  929.       output_file = (char *) xmalloc (s - spec + 1);
  930.       bcopy (spec, output_file, s - spec);
  931.       output_file[s - spec] = 0;
  932.     }
  933.       else
  934.     {
  935.       deps_target = 0;
  936.       output_file = spec;
  937.     }
  938.       
  939.       deps_stream = fopen (output_file, "a");
  940.       if (deps_stream == 0)
  941.     pfatal_with_name (output_file);
  942.     }
  943.   /* If the -M option was used, output the deps to standard output.  */
  944.   else if (print_deps)
  945.     deps_stream = stdout;
  946.  
  947.   /* For -M, print the expected object file name
  948.      as the target of this Make-rule.  */
  949.   if (print_deps) {
  950.     deps_allocated_size = 200;
  951.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  952.     deps_buffer[0] = 0;
  953.     deps_size = 0;
  954.     deps_column = 0;
  955.  
  956.     if (deps_target) {
  957.       deps_output (deps_target, 0);
  958.       deps_output (":", 0);
  959.     } else if (*in_fname == 0)
  960.       deps_output ("-: ", 0);
  961.     else {
  962.       int len;
  963.       char *p = in_fname;
  964.       char *p1 = p;
  965.       /* Discard all directory prefixes from P.  */
  966.       while (*p1) {
  967.     if (*p1 == '/')
  968.       p = p1 + 1;
  969.     p1++;
  970.       }
  971.       /* Output P, but remove known suffixes.  */
  972.       len = strlen (p);
  973.       if (p[len - 2] == '.' && p[len - 1] == 'c')
  974.     deps_output (p, len - 2);
  975.       else if (p[len - 3] == '.'
  976.            && p[len - 2] == 'c'
  977.            && p[len - 1] == 'c')
  978.     deps_output (p, len - 3);
  979.       else
  980.     deps_output (p, 0);
  981.       /* Supply our own suffix.  */
  982.       deps_output (".o : ", 0);
  983.       deps_output (in_fname, 0);
  984.       deps_output (" ", 0);
  985.     }
  986.   }
  987.  
  988.   file_size_and_mode (f, &st_mode, &st_size);
  989.   fp->fname = in_fname;
  990.   fp->lineno = 1;
  991.   /* JF all this is mine about reading pipes and ttys */
  992.   if ((st_mode & S_IFMT) != S_IFREG) {
  993.     /* Read input from a file that is not a normal disk file.
  994.        We cannot preallocate a buffer with the correct size,
  995.        so we must read in the file a piece at the time and make it bigger.  */
  996.     int size;
  997.     int bsize;
  998.     int cnt;
  999.     U_CHAR *bufp;
  1000.  
  1001.     bsize = 2000;
  1002.     size = 0;
  1003.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  1004.     bufp = fp->buf;
  1005.     for (;;) {
  1006.       cnt = read (f, bufp, bsize - size);
  1007.       if (cnt < 0) goto perror;    /* error! */
  1008.       if (cnt == 0) break;    /* End of file */
  1009.       size += cnt;
  1010.       bufp += cnt;
  1011.       if (bsize == size) {    /* Buffer is full! */
  1012.         bsize *= 2;
  1013.         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  1014.     bufp = fp->buf + size;    /* May have moved */
  1015.       }
  1016.     }
  1017.     fp->length = size;
  1018.   } else {
  1019.     /* Read a file whose size we can determine in advance.
  1020.        For the sake of VMS, st_size is just an upper bound.  */
  1021.     long i;
  1022.     fp->length = 0;
  1023.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  1024.  
  1025.     while (st_size > 0) {
  1026.       i = read (f, fp->buf + fp->length, st_size);
  1027.       if (i <= 0) {
  1028.         if (i == 0) break;
  1029.     goto perror;
  1030.       }
  1031.       fp->length += i;
  1032.       st_size -= i;
  1033.     }
  1034.   }
  1035.   fp->bufp = fp->buf;
  1036.   fp->if_stack = if_stack;
  1037.   
  1038.   /* Unless inhibited, convert trigraphs in the input.  */
  1039.  
  1040.   if (!no_trigraphs)
  1041.     trigraph_pcp (fp);
  1042.  
  1043.   /* Make sure data ends with a newline.  And put a null after it.  */
  1044.  
  1045.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  1046.     fp->buf[fp->length++] = '\n';
  1047.   fp->buf[fp->length] = '\0';
  1048.  
  1049.   /* Now that we know the input file is valid, open the output.  */
  1050.  
  1051.   if (!out_fname || !strcmp (out_fname, ""))
  1052.     out_fname = "stdout";
  1053.   else if (! freopen (out_fname, "w", stdout))
  1054.     pfatal_with_name (out_fname);
  1055.  
  1056.   output_line_command (fp, &outbuf, 0, same_file);
  1057.  
  1058.   /* Scan the input, processing macros and directives.  */
  1059.  
  1060.   rescan (&outbuf, 0);
  1061.  
  1062.   /* Now we have processed the entire input
  1063.      Write whichever kind of output has been requested.  */
  1064.  
  1065.  
  1066.   if (dump_macros)
  1067.     dump_all_macros ();
  1068.   else if (! inhibit_output && deps_stream != stdout) {
  1069.     if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
  1070.       fatal ("I/O error on output");
  1071.   }
  1072.  
  1073.   if (print_deps) {
  1074.     fputs (deps_buffer, deps_stream);
  1075.     putc ('\n', deps_stream);
  1076.     if (deps_stream != stdout) {
  1077.       fclose (deps_stream);
  1078.       if (ferror (deps_stream))
  1079.     fatal ("I/O error on output");
  1080.     }
  1081.   }
  1082.  
  1083.   if (ferror (stdout))
  1084.     fatal ("I/O error on output");
  1085.  
  1086.   if (errors)
  1087.     exit (FATAL_EXIT_CODE);
  1088.   exit (SUCCESS_EXIT_CODE);
  1089.  
  1090.  perror:
  1091.   pfatal_with_name (in_fname);
  1092. }
  1093.  
  1094. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  1095.    before main CCCP processing.  Name `pcp' is also in honor of the
  1096.    drugs the trigraph designers must have been on.
  1097.  
  1098.    Using an extra pass through the buffer takes a little extra time,
  1099.    but is infinitely less hairy than trying to handle ??/" inside
  1100.    strings, etc. everywhere, and also makes sure that trigraphs are
  1101.    only translated in the top level of processing. */
  1102.  
  1103. trigraph_pcp (buf)
  1104.      FILE_BUF *buf;
  1105. {
  1106.   register U_CHAR c, *fptr, *bptr, *sptr;
  1107.   int len;
  1108.  
  1109.   fptr = bptr = sptr = buf->buf;
  1110.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  1111.     if (*++sptr != '?')
  1112.       continue;
  1113.     switch (*++sptr) {
  1114.       case '=':
  1115.       c = '#';
  1116.       break;
  1117.     case '(':
  1118.       c = '[';
  1119.       break;
  1120.     case '/':
  1121.       c = '\\';
  1122.       break;
  1123.     case ')':
  1124.       c = ']';
  1125.       break;
  1126.     case '\'':
  1127.       c = '^';
  1128.       break;
  1129.     case '<':
  1130.       c = '{';
  1131.       break;
  1132.     case '!':
  1133.       c = '|';
  1134.       break;
  1135.     case '>':
  1136.       c = '}';
  1137.       break;
  1138.     case '-':
  1139.       c  = '~';
  1140.       break;
  1141.     case '?':
  1142.       sptr--;
  1143.       continue;
  1144.     default:
  1145.       continue;
  1146.     }
  1147.     len = sptr - fptr - 2;
  1148.     if (bptr != fptr && len > 0)
  1149.       bcopy (fptr, bptr, len);    /* BSD doc says bcopy () works right
  1150.                    for overlapping strings.  In ANSI
  1151.                    C, this will be memmove (). */
  1152.     bptr += len;
  1153.     *bptr++ = c;
  1154.     fptr = ++sptr;
  1155.   }
  1156.   len = buf->length - (fptr - buf->buf);
  1157.   if (bptr != fptr && len > 0)
  1158.     bcopy (fptr, bptr, len);
  1159.   buf->length -= fptr - bptr;
  1160.   buf->buf[buf->length] = '\0';
  1161.   if (warn_trigraphs && fptr != bptr)
  1162.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  1163. }
  1164.  
  1165. /* Move all backslash-newline pairs out of embarrassing places.
  1166.    Exchange all such pairs following BP
  1167.    with any potentially-embarrasing characters that follow them.
  1168.    Potentially-embarrassing characters are / and *
  1169.    (because a backslash-newline inside a comment delimiter
  1170.    would cause it not to be recognized).  */
  1171.  
  1172. newline_fix (bp)
  1173.      U_CHAR *bp;
  1174. {
  1175.   register U_CHAR *p = bp;
  1176.   register int count = 0;
  1177.  
  1178.   /* First count the backslash-newline pairs here.  */
  1179.  
  1180.   while (*p++ == '\\' && *p++ == '\n')
  1181.     count++;
  1182.  
  1183.   p = bp + count * 2;
  1184.  
  1185.   /* What follows the backslash-newlines is not embarrassing.  */
  1186.  
  1187.   if (count == 0 || (*p != '/' && *p != '*'))
  1188.     return;
  1189.  
  1190.   /* Copy all potentially embarrassing characters
  1191.      that follow the backslash-newline pairs
  1192.      down to where the pairs originally started.  */
  1193.  
  1194.   while (*p == '*' || *p == '/')
  1195.     *bp++ = *p++;
  1196.  
  1197.   /* Now write the same number of pairs after the embarrassing chars.  */
  1198.   while (count-- > 0) {
  1199.     *bp++ = '\\';
  1200.     *bp++ = '\n';
  1201.   }
  1202. }
  1203.  
  1204. /* Like newline_fix but for use within a directive-name.
  1205.    Move any backslash-newlines up past any following symbol constituents.  */
  1206.  
  1207. name_newline_fix (bp)
  1208.      U_CHAR *bp;
  1209. {
  1210.   register U_CHAR *p = bp;
  1211.   register int count = 0;
  1212.  
  1213.   /* First count the backslash-newline pairs here.  */
  1214.  
  1215.   while (*p++ == '\\' && *p++ == '\n')
  1216.     count++;
  1217.  
  1218.   p = bp + count * 2;
  1219.  
  1220.   /* What follows the backslash-newlines is not embarrassing.  */
  1221.  
  1222.   if (count == 0 || !is_idchar[*p])
  1223.     return;
  1224.  
  1225.   /* Copy all potentially embarrassing characters
  1226.      that follow the backslash-newline pairs
  1227.      down to where the pairs originally started.  */
  1228.  
  1229.   while (is_idchar[*p])
  1230.     *bp++ = *p++;
  1231.  
  1232.   /* Now write the same number of pairs after the embarrassing chars.  */
  1233.   while (count-- > 0) {
  1234.     *bp++ = '\\';
  1235.     *bp++ = '\n';
  1236.   }
  1237. }
  1238.  
  1239. /*
  1240.  * The main loop of the program.
  1241.  *
  1242.  * Read characters from the input stack, transferring them to the
  1243.  * output buffer OP.
  1244.  *
  1245.  * Macros are expanded and push levels on the input stack.
  1246.  * At the end of such a level it is popped off and we keep reading.
  1247.  * At the end of any other kind of level, we return.
  1248.  * #-directives are handled, except within macros.
  1249.  *
  1250.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  1251.  * and insert them when appropriate.  This is set while scanning macro
  1252.  * arguments before substitution.  It is zero when scanning for final output.
  1253.  *   There are three types of Newline markers:
  1254.  *   * Newline -  follows a macro name that was not expanded
  1255.  *     because it appeared inside an expansion of the same macro.
  1256.  *     This marker prevents future expansion of that identifier.
  1257.  *     When the input is rescanned into the final output, these are deleted.
  1258.  *     These are also deleted by ## concatenation.
  1259.  *   * Newline Space (or Newline and any other whitespace character)
  1260.  *     stands for a place that tokens must be separated or whitespace
  1261.  *     is otherwise desirable, but where the ANSI standard specifies there
  1262.  *     is no whitespace.  This marker turns into a Space (or whichever other
  1263.  *     whitespace char appears in the marker) in the final output,
  1264.  *     but it turns into nothing in an argument that is stringified with #.
  1265.  *     Such stringified arguments are the only place where the ANSI standard
  1266.  *     specifies with precision that whitespace may not appear.
  1267.  *
  1268.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  1269.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  1270.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  1271.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  1272.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  1273.  */
  1274.  
  1275. rescan (op, output_marks)
  1276.      FILE_BUF *op;
  1277.      int output_marks;
  1278. {
  1279.   /* Character being scanned in main loop.  */
  1280.   register U_CHAR c;
  1281.  
  1282.   /* Length of pending accumulated identifier.  */
  1283.   register int ident_length = 0;
  1284.  
  1285.   /* Hash code of pending accumulated identifier.  */
  1286.   register int hash = 0;
  1287.  
  1288.   /* Current input level (&instack[indepth]).  */
  1289.   FILE_BUF *ip;
  1290.  
  1291.   /* Pointer for scanning input.  */
  1292.   register U_CHAR *ibp;
  1293.  
  1294.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  1295.   register U_CHAR *limit;
  1296.  
  1297.   /* Pointer for storing output.  */
  1298.   register U_CHAR *obp;
  1299.  
  1300.   /* REDO_CHAR is nonzero if we are processing an identifier
  1301.      after backing up over the terminating character.
  1302.      Sometimes we process an identifier without backing up over
  1303.      the terminating character, if the terminating character
  1304.      is not special.  Backing up is done so that the terminating character
  1305.      will be dispatched on again once the identifier is dealt with.  */
  1306.   int redo_char = 0;
  1307.  
  1308.   /* 1 if within an identifier inside of which a concatenation
  1309.      marker (Newline -) has been seen.  */
  1310.   int concatenated = 0;
  1311.  
  1312.   /* While scanning a comment or a string constant,
  1313.      this records the line it started on, for error messages.  */
  1314.   int start_line;
  1315.  
  1316.   /* Record position of last `real' newline.  */
  1317.   U_CHAR *beg_of_line;
  1318.  
  1319. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  1320.  
  1321. #define POPMACRO \
  1322. do { ip->macro->type = T_MACRO;        \
  1323.      if (ip->free_ptr) free (ip->free_ptr);    \
  1324.      --indepth; } while (0)
  1325.  
  1326. /* Reload `rescan's local variables that describe the current
  1327.    level of the input stack.  */
  1328.  
  1329. #define RECACHE  \
  1330. do { ip = &instack[indepth];        \
  1331.      ibp = ip->bufp;            \
  1332.      limit = ip->buf + ip->length;    \
  1333.      op->bufp = obp;            \
  1334.      check_expand (op, limit - ibp);    \
  1335.      beg_of_line = 0;            \
  1336.      obp = op->bufp; } while (0)
  1337.  
  1338.   if (no_output && instack[indepth].fname != 0)
  1339.     skip_if_group (&instack[indepth], 1);
  1340.  
  1341.   obp = op->bufp;
  1342.   RECACHE;
  1343.   beg_of_line = ibp;
  1344.  
  1345.   /* Our caller must always put a null after the end of
  1346.      the input at each input stack level.  */
  1347.   if (*limit != 0)
  1348.     abort ();
  1349.  
  1350.   while (1) {
  1351.     c = *ibp++;
  1352.     *obp++ = c;
  1353.  
  1354.     switch (c) {
  1355.     case '\\':
  1356.       if (ibp >= limit)
  1357.     break;
  1358.       if (*ibp == '\n') {
  1359.     /* Always merge lines ending with backslash-newline,
  1360.        even in middle of identifier.  */
  1361.     ++ibp;
  1362.     ++ip->lineno;
  1363.     --obp;        /* remove backslash from obuf */
  1364.     break;
  1365.       }
  1366.       /* Otherwise, backslash suppresses specialness of following char,
  1367.      so copy it here to prevent the switch from seeing it.
  1368.      But first get any pending identifier processed.  */
  1369.       if (ident_length > 0)
  1370.     goto specialchar;
  1371.       *obp++ = *ibp++;
  1372.       break;
  1373.  
  1374.     case '#':
  1375.       /* If this is expanding a macro definition, don't recognize
  1376.      preprocessor directives.  */
  1377.       if (ip->macro != 0)
  1378.     goto randomchar;
  1379.       if (ident_length)
  1380.     goto specialchar;
  1381.  
  1382.       /* # keyword: a # must be first nonblank char on the line */
  1383.       if (beg_of_line == 0)
  1384.     goto randomchar;
  1385.       {
  1386.     U_CHAR *bp;
  1387.  
  1388.     /* Scan from start of line, skipping whitespace, comments
  1389.        and backslash-newlines, and see if we reach this #.
  1390.        If not, this # is not special.  */
  1391.     bp = beg_of_line;
  1392.     while (1) {
  1393.       if (is_hor_space[*bp])
  1394.         bp++;
  1395.       else if (*bp == '\\' && bp[1] == '\n')
  1396.         bp += 2;
  1397.       else if (*bp == '/' && bp[1] == '*') {
  1398.         bp += 2;
  1399.         while (!(*bp == '*' && bp[1] == '/'))
  1400.           bp++;
  1401.         bp += 2;
  1402.       }
  1403.       else if (cplusplus && *bp == '/' && bp[1] == '/') {
  1404.         bp += 2;
  1405.         while (*bp++ != '\n') ;
  1406.       }
  1407.       else break;
  1408.     }
  1409.     if (bp + 1 != ibp)
  1410.       goto randomchar;
  1411.       }
  1412.  
  1413.       /* This # can start a directive.  */
  1414.  
  1415.       --obp;        /* Don't copy the '#' */
  1416.  
  1417.       ip->bufp = ibp;
  1418.       op->bufp = obp;
  1419.       if (! handle_directive (ip, op)) {
  1420. #ifdef USE_C_ALLOCA
  1421.     alloca (0);
  1422. #endif
  1423.     /* Not a known directive: treat it as ordinary text.
  1424.        IP, OP, IBP, etc. have not been changed.  */
  1425.     if (no_output && instack[indepth].fname) {
  1426.       /* If not generating expanded output,
  1427.          what we do with ordinary text is skip it.
  1428.          Discard everything until next # directive.  */
  1429.       skip_if_group (&instack[indepth], 1);
  1430.       RECACHE;
  1431.       beg_of_line = ibp;
  1432.       break;
  1433.     }
  1434.     ++obp;        /* Copy the '#' after all */
  1435.     goto randomchar;
  1436.       }
  1437. #ifdef USE_C_ALLOCA
  1438.       alloca (0);
  1439. #endif
  1440.       /* A # directive has been successfully processed.  */
  1441.       /* If not generating expanded output, ignore everything until
  1442.      next # directive.  */
  1443.       if (no_output && instack[indepth].fname)
  1444.     skip_if_group (&instack[indepth], 1);
  1445.       obp = op->bufp;
  1446.       RECACHE;
  1447.       beg_of_line = ibp;
  1448.       break;
  1449.  
  1450.     case '\"':            /* skip quoted string */
  1451.     case '\'':
  1452.       /* A single quoted string is treated like a double -- some
  1453.      programs (e.g., troff) are perverse this way */
  1454.  
  1455.       if (ident_length)
  1456.     goto specialchar;
  1457.  
  1458.       start_line = ip->lineno;
  1459.  
  1460.       /* Skip ahead to a matching quote.  */
  1461.  
  1462.       while (1) {
  1463.     if (ibp >= limit) {
  1464.       if (traditional) {
  1465.         if (ip->macro != 0) {
  1466.           /* try harder: this string crosses a macro expansion boundary */
  1467.           POPMACRO;
  1468.           RECACHE;
  1469.           continue;
  1470.         }
  1471.       } else
  1472.         error_with_line (line_for_error (start_line),
  1473.                  "unterminated string or character constant");
  1474.       break;
  1475.     }
  1476.     *obp++ = *ibp;
  1477.     switch (*ibp++) {
  1478.     case '\n':
  1479.       ++ip->lineno;
  1480.       ++op->lineno;
  1481.       if (traditional)
  1482.         goto while2end;
  1483.       if (pedantic || c == '\'') {
  1484.         error_with_line (line_for_error (start_line),
  1485.                  "unterminated string or character constant");
  1486.         goto while2end;
  1487.       }
  1488.       break;
  1489.  
  1490.     case '\\':
  1491.       if (ibp >= limit)
  1492.         break;
  1493.       if (*ibp == '\n') {
  1494.         /* Backslash newline is replaced by nothing at all,
  1495.            but keep the line counts correct.  */
  1496.         --obp;
  1497.         ++ibp;
  1498.         ++ip->lineno;
  1499.       } else {
  1500.         /* ANSI stupidly requires that in \\ the second \
  1501.            is *not* prevented from combining with a newline.  */
  1502.         while (*ibp == '\\' && ibp[1] == '\n') {
  1503.           ibp += 2;
  1504.           ++ip->lineno;
  1505.         }
  1506.         *obp++ = *ibp++;
  1507.       }
  1508.       break;
  1509.  
  1510.     case '\"':
  1511.     case '\'':
  1512.       if (ibp[-1] == c)
  1513.         goto while2end;
  1514.       break;
  1515.     }
  1516.       }
  1517.     while2end:
  1518.       break;
  1519.  
  1520.     case '/':
  1521.       if (*ibp == '\\' && ibp[1] == '\n')
  1522.     newline_fix (ibp);
  1523.       if (cplusplus && *ibp == '/') {
  1524.     /* C++ style comment... */
  1525.     start_line = ip->lineno;
  1526.  
  1527.     --ibp;            /* Back over the slash */
  1528.     --obp;
  1529.  
  1530.     /* Comments are equivalent to spaces. */
  1531.     if (! put_out_comments)
  1532.       *obp++ = ' ';
  1533.     else {
  1534.       /* must fake up a comment here */
  1535.       *obp++ = '/';
  1536.       *obp++ = '/';
  1537.     }
  1538.     {
  1539.       U_CHAR *before_bp = ibp+2;
  1540.  
  1541.       while (ibp < limit) {
  1542.         if (*ibp++ == '\n') {
  1543.           ibp--;
  1544.           if (put_out_comments) {
  1545.         bcopy (before_bp, obp, ibp - before_bp);
  1546.         obp += ibp - before_bp;
  1547.           }
  1548.           break;
  1549.         }
  1550.       }
  1551.       break;
  1552.     }
  1553.       }
  1554.       if (*ibp != '*')
  1555.     goto randomchar;
  1556.       if (ip->macro != 0)
  1557.     goto randomchar;
  1558.       if (ident_length)
  1559.     goto specialchar;
  1560.  
  1561.       /* We have a comment.  Skip it, optionally copying it to output.  */
  1562.  
  1563.       start_line = ip->lineno;
  1564.  
  1565.       ++ibp;            /* Skip the star. */
  1566.  
  1567.       /* Comments are equivalent to spaces.
  1568.      Note that we already output the slash; we might not want it.
  1569.      For -traditional, a comment is equivalent to nothing.  */
  1570.       if (! put_out_comments) {
  1571.     if (traditional)
  1572.       obp--;
  1573.     else
  1574.       obp[-1] = ' ';
  1575.       }
  1576.       else
  1577.     *obp++ = '*';
  1578.  
  1579.       {
  1580.     U_CHAR *before_bp = ibp;
  1581.  
  1582.     while (ibp < limit) {
  1583.       switch (*ibp++) {
  1584.       case '/':
  1585.         if (warn_comments && ibp < limit && *ibp == '*')
  1586.           warning("`/*' within comment");
  1587.         break;
  1588.       case '*':
  1589.         if (*ibp == '\\' && ibp[1] == '\n')
  1590.           newline_fix (ibp);
  1591.         if (ibp >= limit || *ibp == '/')
  1592.           goto comment_end;
  1593.         break;
  1594.       case '\n':
  1595.         ++ip->lineno;
  1596.         /* Copy the newline into the output buffer, in order to
  1597.            avoid the pain of a #line every time a multiline comment
  1598.            is seen.  */
  1599.         if (!put_out_comments)
  1600.           *obp++ = '\n';
  1601.         ++op->lineno;
  1602.       }
  1603.     }
  1604.       comment_end:
  1605.  
  1606.     if (ibp >= limit)
  1607.       error_with_line (line_for_error (start_line),
  1608.                "unterminated comment");
  1609.     else {
  1610.       ibp++;
  1611.       if (put_out_comments) {
  1612.         bcopy (before_bp, obp, ibp - before_bp);
  1613.         obp += ibp - before_bp;
  1614.       }
  1615.     }
  1616.       }
  1617.       break;
  1618.  
  1619.     case '$':
  1620.       if (!dollars_in_ident)
  1621.     goto randomchar;
  1622.       goto letter;
  1623.  
  1624.     case '0': case '1': case '2': case '3': case '4':
  1625.     case '5': case '6': case '7': case '8': case '9':
  1626.       /* If digit is not part of identifier, it starts a number,
  1627.      which means that following letters are not an identifier.
  1628.      "0x5" does not refer to an identifier "x5".
  1629.      So copy all alphanumerics that follow without accumulating
  1630.      as an identifier.  Periods also, for sake of "3.e7".  */
  1631.  
  1632.       if (ident_length == 0) {
  1633.     while (ibp < limit) {
  1634.       c = *ibp++;
  1635.       if (!isalnum (c) && c != '.' && c != '_') {
  1636.         --ibp;
  1637.         break;
  1638.       }
  1639.       *obp++ = c;
  1640.       /* A sign can be part of a preprocessing number
  1641.          if it follows an e.  */
  1642.       if (c == 'e' || c == 'E') {
  1643.         if (ibp < limit && (*ibp == '+' || *ibp == '-'))
  1644.           *obp++ = *ibp++;
  1645.       }
  1646.     }
  1647.     break;
  1648.       }
  1649.       /* fall through */
  1650.  
  1651.     case '_':
  1652.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  1653.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  1654.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  1655.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  1656.     case 'y': case 'z':
  1657.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1658.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  1659.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  1660.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  1661.     case 'Y': case 'Z':
  1662.     letter:
  1663.       ident_length++;
  1664.       /* Compute step of hash function, to avoid a proc call on every token */
  1665.       hash = HASHSTEP (hash, c);
  1666.       break;
  1667.  
  1668.     case '\n':
  1669.       /* If reprocessing a macro expansion, newline is a special marker.  */
  1670.       if (ip->macro != 0) {
  1671.     /* Newline White is a "funny space" to separate tokens that are
  1672.        supposed to be separate but without space between.
  1673.        Here White means any horizontal whitespace character.
  1674.        Newline - marks a recursive macro use that is not
  1675.        supposed to be expandable.  */
  1676.  
  1677.     if (*ibp == '-') {
  1678.       /* Newline - inhibits expansion of preceding token.
  1679.          If expanding a macro arg, we keep the newline -.
  1680.          In final output, it is deleted.  */
  1681.       if (! concatenated) {
  1682.         ident_length = 0;
  1683.         hash = 0;
  1684.       }
  1685.       ibp++;
  1686.       if (!output_marks) {
  1687.         obp--;
  1688.       } else {
  1689.         /* If expanding a macro arg, keep the newline -.  */
  1690.         *obp++ = '-';
  1691.       }
  1692.     } else if (is_space[*ibp]) {
  1693.       /* Newline Space does not prevent expansion of preceding token
  1694.          so expand the preceding token and then come back.  */
  1695.       if (ident_length > 0)
  1696.         goto specialchar;
  1697.  
  1698.       /* If generating final output, newline space makes a space.  */
  1699.       if (!output_marks) {
  1700.         obp[-1] = *ibp++;
  1701.         /* And Newline Newline makes a newline, so count it.  */
  1702.         if (obp[-1] == '\n')
  1703.           op->lineno++;
  1704.       } else {
  1705.         /* If expanding a macro arg, keep the newline space.
  1706.            If the arg gets stringified, newline space makes nothing.  */
  1707.         *obp++ = *ibp++;
  1708.       }
  1709.     } else abort ();    /* Newline followed by something random?  */
  1710.     break;
  1711.       }
  1712.  
  1713.       /* If there is a pending identifier, handle it and come back here.  */
  1714.       if (ident_length > 0)
  1715.     goto specialchar;
  1716.  
  1717.       beg_of_line = ibp;
  1718.  
  1719.       /* Update the line counts and output a #line if necessary.  */
  1720.       ++ip->lineno;
  1721.       ++op->lineno;
  1722.       if (ip->lineno != op->lineno) {
  1723.     op->bufp = obp;
  1724.     output_line_command (ip, op, 1, same_file);
  1725.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  1726.     obp = op->bufp;
  1727.       }
  1728.       break;
  1729.  
  1730.       /* Come here either after (1) a null character that is part of the input
  1731.      or (2) at the end of the input, because there is a null there.  */
  1732.     case 0:
  1733.       if (ibp <= limit)
  1734.     /* Our input really contains a null character.  */
  1735.     goto randomchar;
  1736.  
  1737.       /* At end of a macro-expansion level, pop it and read next level.  */
  1738.       if (ip->macro != 0) {
  1739.     obp--;
  1740.     ibp--;
  1741.     /* If traditional, and we have an identifier that ends here,
  1742.        process it now, so we get the right error for recursion.  */
  1743.     if (traditional && ident_length
  1744.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  1745.       redo_char = 1;
  1746.       goto randomchar;
  1747.     }
  1748.     POPMACRO;
  1749.     RECACHE;
  1750.     break;
  1751.       }
  1752.  
  1753.       /* If we don't have a pending identifier,
  1754.      return at end of input.  */
  1755.       if (ident_length == 0) {
  1756.     obp--;
  1757.     ibp--;
  1758.     op->bufp = obp;
  1759.     ip->bufp = ibp;
  1760.     goto ending;
  1761.       }
  1762.  
  1763.       /* If we do have a pending identifier, just consider this null
  1764.      a special character and arrange to dispatch on it again.
  1765.      The second time, IDENT_LENGTH will be zero so we will return.  */
  1766.  
  1767.       /* Fall through */
  1768.  
  1769. specialchar:
  1770.  
  1771.       /* Handle the case of a character such as /, ', " or null
  1772.      seen following an identifier.  Back over it so that
  1773.      after the identifier is processed the special char
  1774.      will be dispatched on again.  */
  1775.  
  1776.       ibp--;
  1777.       obp--;
  1778.       redo_char = 1;
  1779.  
  1780.     default:
  1781.  
  1782. randomchar:
  1783.  
  1784.       if (ident_length > 0) {
  1785.     register HASHNODE *hp;
  1786.  
  1787.     /* We have just seen an identifier end.  If it's a macro, expand it.
  1788.  
  1789.        IDENT_LENGTH is the length of the identifier
  1790.        and HASH is its hash code.
  1791.  
  1792.        The identifier has already been copied to the output,
  1793.        so if it is a macro we must remove it.
  1794.  
  1795.        If REDO_CHAR is 0, the char that terminated the identifier
  1796.        has been skipped in the output and the input.
  1797.        OBP-IDENT_LENGTH-1 points to the identifier.
  1798.        If the identifier is a macro, we must back over the terminator.
  1799.  
  1800.        If REDO_CHAR is 1, the terminating char has already been
  1801.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  1802.  
  1803.     for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  1804.          hp = hp->next) {
  1805.  
  1806.       if (hp->length == ident_length) {
  1807.         U_CHAR *obufp_before_macroname;
  1808.         int op_lineno_before_macroname;
  1809.         register int i = ident_length;
  1810.         register U_CHAR *p = hp->name;
  1811.         register U_CHAR *q = obp - i;
  1812.         int disabled;
  1813.  
  1814.         if (! redo_char)
  1815.           q--;
  1816.  
  1817.         do {        /* All this to avoid a strncmp () */
  1818.           if (*p++ != *q++)
  1819.         goto hashcollision;
  1820.         } while (--i);
  1821.  
  1822.         /* We found a use of a macro name.
  1823.            see if the context shows it is a macro call.  */
  1824.  
  1825.         /* Back up over terminating character if not already done.  */
  1826.         if (! redo_char) {
  1827.           ibp--;
  1828.           obp--;
  1829.         }
  1830.  
  1831.         obufp_before_macroname = obp - ident_length;
  1832.         op_lineno_before_macroname = op->lineno;
  1833.  
  1834.         /* Record whether the macro is disabled.  */
  1835.         disabled = hp->type == T_DISABLED;
  1836.  
  1837.         /* This looks like a macro ref, but if the macro was disabled,
  1838.            just copy its name and put in a marker if requested.  */
  1839.  
  1840.         if (disabled) {
  1841. #if 0
  1842.           /* This error check caught useful cases such as
  1843.          #define foo(x,y) bar(x(y,0), y)
  1844.          foo(foo, baz)  */
  1845.           if (traditional)
  1846.         error ("recursive use of macro `%s'", hp->name);
  1847. #endif
  1848.  
  1849.           if (output_marks) {
  1850.         check_expand (op, limit - ibp + 2);
  1851.         *obp++ = '\n';
  1852.         *obp++ = '-';
  1853.           }
  1854.           break;
  1855.         }
  1856.  
  1857.         /* If macro wants an arglist, verify that a '(' follows.
  1858.            first skip all whitespace, copying it to the output
  1859.            after the macro name.  Then, if there is no '(',
  1860.            decide this is not a macro call and leave things that way.  */
  1861.         if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  1862.         && hp->value.defn->nargs >= 0)
  1863.           {
  1864.         while (1) {
  1865.           /* Scan forward over whitespace, copying it to the output.  */
  1866.           if (ibp == limit && ip->macro != 0) {
  1867.             POPMACRO;
  1868.             RECACHE;
  1869.           }
  1870.           /* A comment: copy it to the output unchanged.  */
  1871.           else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  1872.             *obp++ = '/';
  1873.             *obp++ = '*';
  1874.             ibp += 2;
  1875.             while (ibp + 1 != limit
  1876.                && !(ibp[0] == '*' && ibp[1] == '/')) {
  1877.               /* We need not worry about newline-marks,
  1878.              since they are never found in comments.  */
  1879.               if (*ibp == '\n') {
  1880.             /* Newline in a file.  Count it.  */
  1881.             ++ip->lineno;
  1882.             ++op->lineno;
  1883.               }
  1884.               *obp++ = *ibp++;
  1885.             }
  1886.             ibp += 2;
  1887.             *obp++ = '*';
  1888.             *obp++ = '/';
  1889.           }
  1890.           else if (is_space[*ibp]) {
  1891.             *obp++ = *ibp++;
  1892.             if (ibp[-1] == '\n') {
  1893.               if (ip->macro == 0) {
  1894.             /* Newline in a file.  Count it.  */
  1895.             ++ip->lineno;
  1896.             ++op->lineno;
  1897.               } else if (!output_marks) {
  1898.             /* A newline mark, and we don't want marks
  1899.                in the output.  If it is newline-hyphen,
  1900.                discard it entirely.  Otherwise, it is
  1901.                newline-whitechar, so keep the whitechar.  */
  1902.             obp--;
  1903.             if (*ibp == '-')
  1904.               ibp++;
  1905.             else {
  1906.               if (*ibp == '\n')
  1907.                 ++op->lineno;
  1908.               *obp++ = *ibp++;
  1909.             }
  1910.               } else {
  1911.             /* A newline mark; copy both chars to the output.  */
  1912.             *obp++ = *ibp++;
  1913.               }
  1914.             }
  1915.           }
  1916.           else break;
  1917.         }
  1918.         if (*ibp != '(')
  1919.           break;
  1920.           }
  1921.  
  1922.         /* This is now known to be a macro call.
  1923.            Discard the macro name from the output,
  1924.            along with any following whitespace just copied.  */
  1925.         obp = obufp_before_macroname;
  1926.         op->lineno = op_lineno_before_macroname;
  1927.  
  1928.         /* Expand the macro, reading arguments as needed,
  1929.            and push the expansion on the input stack.  */
  1930.         ip->bufp = ibp;
  1931.         op->bufp = obp;
  1932.         macroexpand (hp, op);
  1933.  
  1934.         /* Reexamine input stack, since macroexpand has pushed
  1935.            a new level on it.  */
  1936.         obp = op->bufp;
  1937.         RECACHE;
  1938.         break;
  1939.       }
  1940. hashcollision:
  1941.            ;
  1942.     }            /* End hash-table-search loop */
  1943.     ident_length = hash = 0; /* Stop collecting identifier */
  1944.     redo_char = 0;
  1945.     concatenated = 0;
  1946.       }                /* End if (ident_length > 0) */
  1947.     }                /* End switch */
  1948.   }                /* End per-char loop */
  1949.  
  1950.   /* Come here to return -- but first give an error message
  1951.      if there was an unterminated successful conditional.  */
  1952.  ending:
  1953.   if (if_stack != ip->if_stack) {
  1954.     char *str;
  1955.     switch (if_stack->type) {
  1956.     case T_IF:
  1957.       str = "if";
  1958.       break;
  1959.     case T_IFDEF:
  1960.       str = "ifdef";
  1961.       break;
  1962.     case T_IFNDEF:
  1963.       str = "ifndef";
  1964.       break;
  1965.     case T_ELSE:
  1966.       str = "else";
  1967.       break;
  1968.     case T_ELIF:
  1969.       str = "elif";
  1970.       break;
  1971.     }
  1972.     error_with_line (line_for_error (if_stack->lineno),
  1973.              "unterminated #%s conditional", str);
  1974.   }
  1975.   if_stack = ip->if_stack;
  1976. }
  1977.  
  1978. /*
  1979.  * Rescan a string into a temporary buffer and return the result
  1980.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  1981.  *
  1982.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  1983.  * and insert such markers when appropriate.  See `rescan' for details.
  1984.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  1985.  * before substitution; it is 0 for other uses.
  1986.  */
  1987. FILE_BUF
  1988. expand_to_temp_buffer (buf, limit, output_marks)
  1989.      U_CHAR *buf, *limit;
  1990.      int output_marks;
  1991. {
  1992.   register FILE_BUF *ip;
  1993.   FILE_BUF obuf;
  1994.   int length = limit - buf;
  1995.   U_CHAR *buf1;
  1996.   int odepth = indepth;
  1997.  
  1998.   if (length < 0)
  1999.     abort ();
  2000.  
  2001.   /* Set up the input on the input stack.  */
  2002.  
  2003.   buf1 = (U_CHAR *) alloca (length + 1);
  2004.   {
  2005.     register U_CHAR *p1 = buf;
  2006.     register U_CHAR *p2 = buf1;
  2007.  
  2008.     while (p1 != limit)
  2009.       *p2++ = *p1++;
  2010.   }
  2011.   buf1[length] = 0;
  2012.  
  2013.   /* Set up to receive the output.  */
  2014.  
  2015.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  2016.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  2017.   obuf.fname = 0;
  2018.   obuf.macro = 0;
  2019.   obuf.free_ptr = 0;
  2020.  
  2021.   CHECK_DEPTH ({return obuf;});
  2022.  
  2023.   ++indepth;
  2024.  
  2025.   ip = &instack[indepth];
  2026.   ip->fname = 0;
  2027.   ip->macro = 0;
  2028.   ip->free_ptr = 0;
  2029.   ip->length = length;
  2030.   ip->buf = ip->bufp = buf1;
  2031.   ip->if_stack = if_stack;
  2032.  
  2033.   ip->lineno = obuf.lineno = 1;
  2034.  
  2035.   /* Scan the input, create the output.  */
  2036.  
  2037.   rescan (&obuf, output_marks);
  2038.  
  2039.   /* Pop input stack to original state.  */
  2040.   --indepth;
  2041.  
  2042.   if (indepth != odepth)
  2043.     abort ();
  2044.  
  2045.   /* Record the output.  */
  2046.   obuf.length = obuf.bufp - obuf.buf;
  2047.  
  2048.   return obuf;
  2049. }
  2050.  
  2051. /*
  2052.  * Process a # directive.  Expects IP->bufp to point to the '#', as in
  2053.  * `#define foo bar'.  Passes to the command handler
  2054.  * (do_define, do_include, etc.): the addresses of the 1st and
  2055.  * last chars of the command (starting immediately after the #
  2056.  * keyword), plus op and the keyword table pointer.  If the command
  2057.  * contains comments it is copied into a temporary buffer sans comments
  2058.  * and the temporary buffer is passed to the command handler instead.
  2059.  * Likewise for backslash-newlines.
  2060.  *
  2061.  * Returns nonzero if this was a known # directive.
  2062.  * Otherwise, returns zero, without advancing the input pointer.
  2063.  */
  2064.  
  2065. int
  2066. handle_directive (ip, op)
  2067.      FILE_BUF *ip, *op;
  2068. {
  2069.   register U_CHAR *bp, *cp;
  2070.   register struct directive *kt;
  2071.   register int ident_length;
  2072.   U_CHAR *resume_p;
  2073.  
  2074.   /* Nonzero means we must copy the entire command
  2075.      to get rid of comments or backslash-newlines.  */
  2076.   int copy_command = 0;
  2077.  
  2078.   U_CHAR *ident, *after_ident;
  2079.  
  2080.   bp = ip->bufp;
  2081.   /* Skip whitespace and \-newline.  */
  2082.   while (1) {
  2083.     if (is_hor_space[*bp])
  2084.       bp++;
  2085.     else if (*bp == '/' && bp[1] == '*') {
  2086.       ip->bufp = bp;
  2087.       skip_to_end_of_comment (ip, &ip->lineno);
  2088.       bp = ip->bufp;
  2089.     } else if (*bp == '\\' && bp[1] == '\n') {
  2090.       bp += 2; ip->lineno++;
  2091.     } else break;
  2092.   }
  2093.  
  2094.   /* Now find end of directive name.
  2095.      If we encounter a backslash-newline, exchange it with any following
  2096.      symbol-constituents so that we end up with a contiguous name.  */
  2097.  
  2098.   cp = bp;
  2099.   while (1) {
  2100.     if (is_idchar[*cp])
  2101.       cp++;
  2102.     else {
  2103.       if (*cp == '\\' && cp[1] == '\n')
  2104.     name_newline_fix (cp);
  2105.       if (is_idchar[*cp])
  2106.     cp++;
  2107.       else break;
  2108.     }
  2109.   }
  2110.   ident_length = cp - bp;
  2111.   ident = bp;
  2112.   after_ident = cp;
  2113.  
  2114.   /* A line of just `#' becomes blank.  */
  2115.  
  2116.   if (traditional && ident_length == 0 && *after_ident == '\n') {
  2117.     ip->bufp = after_ident;
  2118.     return 1;
  2119.   }
  2120.  
  2121.   /*
  2122.    * Decode the keyword and call the appropriate expansion
  2123.    * routine, after moving the input pointer up to the next line.
  2124.    */
  2125.   for (kt = directive_table; kt->length > 0; kt++) {
  2126.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  2127.       register U_CHAR *buf;
  2128.       register U_CHAR *limit = ip->buf + ip->length;
  2129.       int unterminated = 0;
  2130.  
  2131.       /* Nonzero means do not delete comments within the directive.
  2132.      #define needs this when -traditional.  */
  2133.       int keep_comments = traditional && kt->traditional_comments;
  2134.  
  2135.       /* Find the end of this command (first newline not backslashed
  2136.      and not in a string or comment).
  2137.      Set COPY_COMMAND if the command must be copied
  2138.      (it contains a backslash-newline or a comment).  */
  2139.  
  2140.       buf = bp = after_ident;
  2141.       while (bp < limit) {
  2142.     register U_CHAR c = *bp++;
  2143.     switch (c) {
  2144.     case '\\':
  2145.       if (bp < limit) {
  2146.         if (*bp == '\n') {
  2147.           ip->lineno++;
  2148.           copy_command = 1;
  2149.         }
  2150.         bp++;
  2151.       }
  2152.       break;
  2153.  
  2154.     case '\'':
  2155.     case '\"':
  2156.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  2157.       /* Don't bother calling the directive if we already got an error
  2158.          message due to unterminated string.  Skip everything and pretend
  2159.          we called the directive.  */
  2160.       if (unterminated) {
  2161.         if (traditional) {
  2162.           /* Traditional preprocessing permits unterminated strings.  */
  2163.           --bp;
  2164.           ip->bufp = bp;
  2165.           goto endloop1;
  2166.         }
  2167.         ip->bufp = bp;
  2168.         return 1;
  2169.       }
  2170.       break;
  2171.  
  2172.       /* <...> is special for #include.  */
  2173.     case '<':
  2174.       if (!kt->angle_brackets)
  2175.         break;
  2176.       while (*bp && *bp != '>') bp++;
  2177.       break;
  2178.  
  2179.     case '/':
  2180.       if (*bp == '\\' && bp[1] == '\n')
  2181.         newline_fix (bp);
  2182.       if (*bp == '*'
  2183.           || (cplusplus && *bp == '/')) {
  2184.         U_CHAR *obp = bp - 1;
  2185.         ip->bufp = bp + 1;
  2186.         skip_to_end_of_comment (ip, &ip->lineno);
  2187.         bp = ip->bufp;
  2188.         /* No need to copy the command because of a comment at the end;
  2189.            just don't include the comment in the directive.  */
  2190.         if (bp == limit || *bp == '\n') {
  2191.           bp = obp;
  2192.           goto endloop1;
  2193.         }
  2194.         /* Don't remove the comments if -traditional.  */
  2195.         if (! keep_comments)
  2196.           copy_command++;
  2197.       }
  2198.       break;
  2199.  
  2200.     case '\n':
  2201.       --bp;        /* Point to the newline */
  2202.       ip->bufp = bp;
  2203.       goto endloop1;
  2204.     }
  2205.       }
  2206.       ip->bufp = bp;
  2207.  
  2208.     endloop1:
  2209.       resume_p = ip->bufp;
  2210.       /* BP is the end of the directive.
  2211.      RESUME_P is the next interesting data after the directive.
  2212.      A comment may come between.  */
  2213.  
  2214.       if (copy_command) {
  2215.     register U_CHAR *xp = buf;
  2216.     /* Need to copy entire command into temp buffer before dispatching */
  2217.  
  2218.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  2219.                           some slop */
  2220.     buf = cp;
  2221.  
  2222.     /* Copy to the new buffer, deleting comments
  2223.        and backslash-newlines (and whitespace surrounding the latter).  */
  2224.  
  2225.     while (xp < bp) {
  2226.       register U_CHAR c = *xp++;
  2227.       *cp++ = c;
  2228.  
  2229.       switch (c) {
  2230.       case '\n':
  2231.         break;
  2232.  
  2233.         /* <...> is special for #include.  */
  2234.       case '<':
  2235.         if (!kt->angle_brackets)
  2236.           break;
  2237.         while (xp < bp && c != '>') {
  2238.           c = *xp++;
  2239.           *cp++ = c;
  2240.         }
  2241.         break;
  2242.  
  2243.       case '\\':
  2244.         if (*xp == '\n') {
  2245.           xp++;
  2246.           cp--;
  2247.           if (cp != buf && is_space[cp[-1]]) {
  2248.         while (cp != buf && is_space[cp[-1]]) cp--;
  2249.         cp++;
  2250.         SKIP_WHITE_SPACE (xp);
  2251.           } else if (is_space[*xp]) {
  2252.         *cp++ = *xp++;
  2253.         SKIP_WHITE_SPACE (xp);
  2254.           }
  2255.         }
  2256.         break;
  2257.  
  2258.       case '\'':
  2259.       case '\"':
  2260.         if (!traditional) {
  2261.           register U_CHAR *bp1
  2262.         = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
  2263.           while (xp != bp1)
  2264.         *cp++ = *xp++;
  2265.         }
  2266.         break;
  2267.  
  2268.       case '/':
  2269.         if (keep_comments)
  2270.           break;
  2271.         if (*xp == '*'
  2272.         || (cplusplus && *xp == '/')) {
  2273.           if (traditional)
  2274.         cp--;
  2275.           else
  2276.         cp[-1] = ' ';
  2277.           ip->bufp = xp + 1;
  2278.           skip_to_end_of_comment (ip, 0);
  2279.           xp = ip->bufp;
  2280.         }
  2281.       }
  2282.     }
  2283.  
  2284.     /* Null-terminate the copy.  */
  2285.  
  2286.     *cp = 0;
  2287.       }
  2288.       else
  2289.     cp = bp;
  2290.  
  2291.       ip->bufp = resume_p;
  2292.  
  2293.       /* Some directives should be written out for cc1 to process,
  2294.      just as if they were not defined.  */
  2295.  
  2296.       if (kt->pass_thru) {
  2297.         int len;
  2298.  
  2299.     /* Output directive name.  */
  2300.         check_expand (op, kt->length+1);
  2301.         *op->bufp++ = '#';
  2302.         bcopy (kt->name, op->bufp, kt->length);
  2303.         op->bufp += kt->length;
  2304.  
  2305.     /* Output arguments.  */
  2306.         len = (cp - buf);
  2307.         check_expand (op, len);
  2308.         bcopy (buf, op->bufp, len);
  2309.         op->bufp += len;
  2310.       }
  2311.  
  2312.       /* Call the appropriate command handler.  buf now points to
  2313.      either the appropriate place in the input buffer, or to
  2314.      the temp buffer if it was necessary to make one.  cp
  2315.      points to the first char after the contents of the (possibly
  2316.      copied) command, in either case. */
  2317.       (*kt->func) (buf, cp, op, kt);
  2318.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  2319.  
  2320.       return 1;
  2321.     }
  2322.   }
  2323.  
  2324.   return 0;
  2325. }
  2326.  
  2327. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2328.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  2329.                 };
  2330.  
  2331. /*
  2332.  * expand things like __FILE__.  Place the expansion into the output
  2333.  * buffer *without* rescanning.
  2334.  */
  2335. special_symbol (hp, op)
  2336.      HASHNODE *hp;
  2337.      FILE_BUF *op;
  2338. {
  2339.   char *buf;
  2340.   long t;
  2341.   int i, len;
  2342.   int true_indepth;
  2343.   FILE_BUF *ip = NULL;
  2344.   static struct tm *timebuf = NULL;
  2345.   struct tm *localtime ();
  2346.  
  2347.   int paren = 0;        /* For special `defined' keyword */
  2348.  
  2349.   for (i = indepth; i >= 0; i--)
  2350.     if (instack[i].fname != NULL) {
  2351.       ip = &instack[i];
  2352.       break;
  2353.     }
  2354.   if (ip == NULL) {
  2355.     error ("cccp error: not in any file?!");
  2356.     return;            /* the show must go on */
  2357.   }
  2358.  
  2359.   switch (hp->type) {
  2360.   case T_FILE:
  2361.   case T_BASE_FILE:
  2362.     {
  2363.       char *string;
  2364.       if (hp->type == T_FILE)
  2365.     string = ip->fname;
  2366.       else
  2367.     string = instack[0].fname;
  2368.  
  2369.       if (string)
  2370.     {
  2371.       buf = (char *) alloca (3 + strlen (string));
  2372.       sprintf (buf, "\"%s\"", string);
  2373.     }
  2374.       else
  2375.     buf = "\"\"";
  2376.  
  2377.       break;
  2378.     }
  2379.  
  2380.   case T_INCLUDE_LEVEL:
  2381.     true_indepth = 0;
  2382.     for (i = indepth; i >= 0; i--)
  2383.       if (instack[i].fname != NULL)
  2384.         true_indepth++;
  2385.  
  2386.     buf = (char *) alloca (8);    /* Eigth bytes ought to be more than enough */
  2387.     sprintf (buf, "%d", true_indepth - 1);
  2388.     break;
  2389.  
  2390.   case T_VERSION:
  2391.     buf = (char *) alloca (3 + strlen (version_string));
  2392.     sprintf (buf, "\"%s\"", version_string);
  2393.     break;
  2394.  
  2395.   case T_CONST:
  2396.     buf = (char *) alloca (4 * sizeof (int));
  2397.     sprintf (buf, "%d", hp->value.ival);
  2398.     break;
  2399.  
  2400.   case T_SPECLINE:
  2401.     buf = (char *) alloca (10);
  2402.     sprintf (buf, "%d", ip->lineno);
  2403.     break;
  2404.  
  2405.   case T_DATE:
  2406.   case T_TIME:
  2407.     if (timebuf == NULL) {
  2408.       t = time (0);
  2409.       timebuf = localtime (&t);
  2410.     }
  2411.     buf = (char *) alloca (20);
  2412.     if (hp->type == T_DATE)
  2413.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  2414.           timebuf->tm_mday, timebuf->tm_year + 1900);
  2415.     else
  2416.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  2417.           timebuf->tm_sec);
  2418.     break;
  2419.  
  2420.   case T_SPEC_DEFINED:
  2421.     buf = " 0 ";        /* Assume symbol is not defined */
  2422.     ip = &instack[indepth];
  2423.     SKIP_WHITE_SPACE (ip->bufp);
  2424.     if (*ip->bufp == '(') {
  2425.       paren++;
  2426.       ip->bufp++;            /* Skip over the paren */
  2427.       SKIP_WHITE_SPACE (ip->bufp);
  2428.     }
  2429.  
  2430.     if (!is_idstart[*ip->bufp])
  2431.       goto oops;
  2432.     if (lookup (ip->bufp, -1, -1))
  2433.       buf = " 1 ";
  2434.     while (is_idchar[*ip->bufp])
  2435.       ++ip->bufp;
  2436.     SKIP_WHITE_SPACE (ip->bufp);
  2437.     if (paren) {
  2438.       if (*ip->bufp != ')')
  2439.     goto oops;
  2440.       ++ip->bufp;
  2441.     }
  2442.     break;
  2443.  
  2444. oops:
  2445.  
  2446.     error ("`defined' must be followed by ident or (ident)");
  2447.     break;
  2448.  
  2449.   default:
  2450.     error ("cccp error: invalid special hash type"); /* time for gdb */
  2451.     abort ();
  2452.   }
  2453.   len = strlen (buf);
  2454.   check_expand (op, len);
  2455.   bcopy (buf, op->bufp, len);
  2456.   op->bufp += len;
  2457.  
  2458.   return;
  2459. }
  2460.  
  2461.  
  2462. /* Routines to handle #directives */
  2463.  
  2464. /*
  2465.  * Process include file by reading it in and calling rescan.
  2466.  * Expects to see "fname" or <fname> on the input.
  2467.  */
  2468.  
  2469. do_include (buf, limit, op, keyword)
  2470.      U_CHAR *buf, *limit;
  2471.      FILE_BUF *op;
  2472.      struct directive *keyword;
  2473. {
  2474.   char *fname;        /* Dynamically allocated fname buffer */
  2475.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  2476.  
  2477.   struct file_name_list *stackp = include; /* Chain of dirs to search */
  2478.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  2479.   int flen;
  2480.  
  2481.   int f;            /* file number */
  2482.  
  2483.   int retried = 0;        /* Have already tried macro
  2484.                    expanding the include line*/
  2485.   FILE_BUF trybuf;        /* It got expanded into here */
  2486.   int system_header_p = 0;    /* 0 for "...", 1 for <...> */
  2487.  
  2488.   f= -1;            /* JF we iz paranoid! */
  2489.  
  2490. get_filename:
  2491.  
  2492.   fbeg = buf;
  2493.   SKIP_WHITE_SPACE (fbeg);
  2494.   /* Discard trailing whitespace so we can easily see
  2495.      if we have parsed all the significant chars we were given.  */
  2496.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  2497.  
  2498.   switch (*fbeg++) {
  2499.   case '\"':
  2500.     fend = fbeg;
  2501.     while (fend != limit && *fend != '\"')
  2502.       fend++;
  2503.     if (*fend == '\"' && fend + 1 == limit) {
  2504.       FILE_BUF *fp;
  2505.  
  2506.       /* We have "filename".  Figure out directory this source
  2507.      file is coming from and put it on the front of the list. */
  2508.  
  2509.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  2510.       if (ignore_srcdir) break;
  2511.  
  2512.       for (fp = &instack[indepth]; fp >= instack; fp--)
  2513.     {
  2514.       int n;
  2515.       char *ep,*nam;
  2516.       extern char *rindex ();
  2517.  
  2518.       if ((nam = fp->fname) != NULL) {
  2519.         /* Found a named file.  Figure out dir of the file,
  2520.            and put it in front of the search list.  */
  2521.         dsp[0].next = stackp;
  2522.         stackp = dsp;
  2523. #ifndef VMS
  2524.         ep = rindex (nam, '/');
  2525. #else                /* VMS */
  2526.         ep = rindex (nam, ']');
  2527.         if (ep == NULL) ep = rindex (nam, '>');
  2528.         if (ep == NULL) ep = rindex (nam, ':');
  2529.         if (ep != NULL) ep++;
  2530. #endif                /* VMS */
  2531.         if (ep != NULL) {
  2532.           n = ep - nam;
  2533.           dsp[0].fname = (char *) alloca (n + 1);
  2534.           strncpy (dsp[0].fname, nam, n);
  2535.           dsp[0].fname[n] = '\0';
  2536.           if (n > max_include_len) max_include_len = n;
  2537.         } else {
  2538.           dsp[0].fname = 0; /* Current directory */
  2539.         }
  2540.         break;
  2541.       }
  2542.     }
  2543.       break;
  2544.     }
  2545.     goto fail;
  2546.  
  2547.   case '<':
  2548.     fend = fbeg;
  2549.     while (fend != limit && *fend != '>') fend++;
  2550.     if (*fend == '>' && fend + 1 == limit) {
  2551.       system_header_p = 1;
  2552.       /* If -I-, start with the first -I dir after the -I-.  */
  2553.       if (first_bracket_include)
  2554.     stackp = first_bracket_include;
  2555.       break;
  2556.     }
  2557.     goto fail;
  2558.  
  2559.   default:
  2560.   fail:
  2561.     if (retried) {
  2562.       error ("#include expects \"fname\" or <fname>");
  2563.       return;
  2564.     } else {
  2565.       trybuf = expand_to_temp_buffer (buf, limit, 0);
  2566.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  2567.       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  2568.       limit = buf + (trybuf.bufp - trybuf.buf);
  2569.       free (trybuf.buf);
  2570.       retried++;
  2571.       goto get_filename;
  2572.     }
  2573.   }
  2574.  
  2575.   flen = fend - fbeg;
  2576.   fname = (char *) alloca (max_include_len + flen + 2);
  2577.   /* + 2 above for slash and terminating null.  */
  2578.  
  2579.   /* If specified file name is absolute, just open it.  */
  2580.  
  2581.   if (*fbeg == '/') {
  2582.     strncpy (fname, fbeg, flen);
  2583.     fname[flen] = 0;
  2584.     f = open (fname, O_RDONLY, 0666);
  2585.   } else {
  2586.     /* Search directory path, trying to open the file.
  2587.        Copy each filename tried into FNAME.  */
  2588.  
  2589.     for (; stackp; stackp = stackp->next) {
  2590.       if (stackp->fname) {
  2591.     if (strlen(stackp->fname) > max_include_len) {
  2592.         fprintf(stderr,"Error in cpc: \"%s\" unexpectedly long\n",
  2593.             stackp->fname);
  2594.         exit(-1);
  2595.     }
  2596.     strcpy (fname, stackp->fname);
  2597.     strcat (fname, "/");
  2598.     fname[strlen (fname) + flen] = 0;
  2599.       } else {
  2600.     fname[0] = 0;
  2601.       }
  2602.       strncat (fname, fbeg, flen);
  2603. #ifdef VMS
  2604.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  2605.          full VMS file specification */
  2606.       if (stackp->fname && (stackp->fname[0] != 0)) {
  2607.     /* Fix up the filename */
  2608.     hack_vms_include_specification (fname);
  2609.       } else {
  2610.           /* This is a normal VMS filespec, so use it unchanged.  */
  2611.     strncpy (fname, fbeg, flen);
  2612.     fname[flen] = 0;
  2613.       }
  2614. #endif /* VMS */
  2615.       if ((f = open (fname, O_RDONLY, 0666)) >= 0)
  2616.     break;
  2617.     }
  2618.   }
  2619.  
  2620.   if (f < 0) {
  2621.     strncpy (fname, fbeg, flen);
  2622.     fname[flen] = 0;
  2623.     error_from_errno (fname);
  2624.  
  2625.     /* For -M, add this file to the dependencies.  */
  2626.     if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2627.       if (system_header_p)
  2628.     warning ("nonexistent file <%.*s> omitted from dependency output",
  2629.          fend - fbeg, fbeg);
  2630.       else
  2631.     {
  2632.       deps_output (fbeg, fend - fbeg);
  2633.       deps_output (" ", 0);
  2634.     }
  2635.     }
  2636.   } else {
  2637.  
  2638.     /* Check to see if this include file is a once-only include file.
  2639.        If so, give up.  */
  2640.  
  2641.     struct file_name_list* ptr;
  2642.  
  2643.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  2644.       if (!strcmp (ptr->fname, fname)) {
  2645.     close (f);
  2646.         return;                /* This file was once'd. */
  2647.       }
  2648.     }
  2649.  
  2650.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  2651.       if (!strcmp (ptr->fname, fname))
  2652.         break;                /* This file was included before. */
  2653.     }
  2654.  
  2655.     if (ptr == 0) {
  2656.       /* This is the first time for this file.  */
  2657.       /* Add it to list of files included.  */
  2658.  
  2659.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2660.       ptr->next = all_include_files;
  2661.       all_include_files = ptr;
  2662.       ptr->fname = savestring (fname);
  2663.  
  2664.       /* For -M, add this file to the dependencies.  */
  2665.       if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2666.     deps_output (fname, strlen (fname));
  2667.     deps_output (" ", 0);
  2668.       }
  2669.     }   
  2670.  
  2671.     if (system_header_p)
  2672.       system_include_depth++;
  2673.  
  2674.     /* Actually process the file.  */
  2675.     finclude (f, fname, op);
  2676.  
  2677.     if (system_header_p)
  2678.       system_include_depth--;
  2679.  
  2680.     close (f);
  2681.   }
  2682. }
  2683.  
  2684. /* Process the contents of include file FNAME, already open on descriptor F,
  2685.    with output to OP.  */
  2686.  
  2687. finclude (f, fname, op)
  2688.      int f;
  2689.      char *fname;
  2690.      FILE_BUF *op;
  2691. {
  2692.   int st_mode;
  2693.   long st_size;
  2694.   long i;
  2695.   FILE_BUF *fp;            /* For input stack frame */
  2696.   int success = 0;
  2697.  
  2698.   CHECK_DEPTH (return;);
  2699.  
  2700.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  2701.     goto nope;        /* Impossible? */
  2702.  
  2703.   fp = &instack[indepth + 1];
  2704.   bzero (fp, sizeof (FILE_BUF));
  2705.   fp->fname = fname;
  2706.   fp->length = 0;
  2707.   fp->lineno = 1;
  2708.   fp->if_stack = if_stack;
  2709.  
  2710.   if (st_mode & S_IFREG) {
  2711.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2712.     fp->bufp = fp->buf;
  2713.  
  2714.     /* Read the file contents, knowing that st_size is an upper bound
  2715.        on the number of bytes we can read.  */
  2716.     while (st_size > 0) {
  2717.       i = read (f, fp->buf + fp->length, st_size);
  2718.       if (i <= 0) {
  2719.     if (i == 0) break;
  2720.     goto nope;
  2721.       }
  2722.       fp->length += i;
  2723.       st_size -= i;
  2724.     }
  2725.   }
  2726.   else {
  2727.     /* Cannot count its file size before reading.
  2728.        First read the entire file into heap and
  2729.        copy them into buffer on stack. */
  2730.  
  2731.     U_CHAR *bufp;
  2732.     U_CHAR *basep;
  2733.     int bsize = 2000;
  2734.  
  2735.     st_size = 0;
  2736.     basep = (U_CHAR *) xmalloc (bsize + 2);
  2737.     bufp = basep;
  2738.  
  2739.     for (;;) {
  2740.       i = read (f, bufp, bsize - st_size);
  2741.       if (i < 0)
  2742.     goto nope;      /* error! */
  2743.       if (i == 0)
  2744.     break;    /* End of file */
  2745.       st_size += i;
  2746.       bufp += i;
  2747.       if (bsize == st_size) {    /* Buffer is full! */
  2748.       bsize *= 2;
  2749.       basep = (U_CHAR *) xrealloc (basep, bsize + 2);
  2750.       bufp = basep + st_size;    /* May have moved */
  2751.     }
  2752.     }
  2753.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2754.     fp->bufp = fp->buf;
  2755.     bcopy (basep, fp->buf, st_size);
  2756.     fp->length = st_size;
  2757.     free (basep);
  2758.   }
  2759.  
  2760.   if (!no_trigraphs)
  2761.     trigraph_pcp (fp);
  2762.  
  2763.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  2764.     fp->buf[fp->length++] = '\n';
  2765.   fp->buf[fp->length] = '\0';
  2766.  
  2767.   success = 1;
  2768.   indepth++;
  2769.  
  2770.   output_line_command (fp, op, 0, enter_file);
  2771.   rescan (op, 0);
  2772.   indepth--;
  2773.   output_line_command (&instack[indepth], op, 0, leave_file);
  2774.  
  2775. nope:
  2776.  
  2777.   if (!success)
  2778.     perror_with_name (fname);
  2779.  
  2780.   close (f);
  2781. }
  2782.  
  2783. /* The arglist structure is built by do_define to tell
  2784.    collect_definition where the argument names begin.  That
  2785.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  2786.    would contain pointers to the strings x, y, and z.
  2787.    Collect_definition would then build a DEFINITION node,
  2788.    with reflist nodes pointing to the places x, y, and z had
  2789.    appeared.  So the arglist is just convenience data passed
  2790.    between these two routines.  It is not kept around after
  2791.    the current #define has been processed and entered into the
  2792.    hash table. */
  2793.  
  2794. struct arglist {
  2795.   struct arglist *next;
  2796.   U_CHAR *name;
  2797.   int length;
  2798.   int argno;
  2799. };
  2800.  
  2801. /* Process a #define command.
  2802. BUF points to the contents of the #define command, as a continguous string.
  2803. LIMIT points to the first character past the end of the definition.
  2804. KEYWORD is the keyword-table entry for #define.  */
  2805.  
  2806. do_define (buf, limit, op, keyword)
  2807.      U_CHAR *buf, *limit;
  2808.      FILE_BUF *op;
  2809.      struct directive *keyword;
  2810. {
  2811.   U_CHAR *bp;            /* temp ptr into input buffer */
  2812.   U_CHAR *symname;        /* remember where symbol name starts */
  2813.   int sym_length;        /* and how long it is */
  2814.  
  2815.   DEFINITION *defn;
  2816.   int arglengths = 0;        /* Accumulate lengths of arg names
  2817.                    plus number of args.  */
  2818.   int hashcode;
  2819.  
  2820.   bp = buf;
  2821.  
  2822.   while (is_hor_space[*bp])
  2823.     bp++;
  2824.   if (!is_idstart[*bp])
  2825.     warning ("macro name starts with a digit");
  2826.  
  2827.   symname = bp;            /* remember where it starts */
  2828.   while (is_idchar[*bp] && bp < limit) {
  2829.     bp++;
  2830.   }
  2831.   sym_length = bp - symname;
  2832.  
  2833.   /* lossage will occur if identifiers or control keywords are broken
  2834.      across lines using backslash.  This is not the right place to take
  2835.      care of that. */
  2836.  
  2837.   if (*bp == '(') {
  2838.     struct arglist *arg_ptrs = NULL;
  2839.     int argno = 0;
  2840.  
  2841.     bp++;            /* skip '(' */
  2842.     SKIP_WHITE_SPACE (bp);
  2843.  
  2844.     /* Loop over macro argument names.  */
  2845.     while (*bp != ')') {
  2846.       struct arglist *temp;
  2847.  
  2848.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  2849.       temp->name = bp;
  2850.       temp->next = arg_ptrs;
  2851.       temp->argno = argno++;
  2852.       arg_ptrs = temp;
  2853.  
  2854.       if (!is_idstart[*bp])
  2855.     warning ("parameter name starts with a digit in #define");
  2856.  
  2857.       /* Find the end of the arg name.  */
  2858.       while (is_idchar[*bp]) {
  2859.     bp++;
  2860.       }
  2861.       temp->length = bp - temp->name;
  2862.       arglengths += temp->length + 2;
  2863.       SKIP_WHITE_SPACE (bp);
  2864.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  2865.     error ("badly punctuated parameter list in #define");
  2866.     goto nope;
  2867.       }
  2868.       if (*bp == ',') {
  2869.     bp++;
  2870.     SKIP_WHITE_SPACE (bp);
  2871.       }
  2872.       if (bp >= limit) {
  2873.     error ("unterminated parameter list in #define");
  2874.     goto nope;
  2875.       }
  2876.     }
  2877.  
  2878.     ++bp;            /* skip paren */
  2879.     /* Skip exactly one space or tab if any.  */
  2880.     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  2881.     /* now everything from bp before limit is the definition. */
  2882.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  2883.  
  2884.     /* Now set defn->argnames to the result of concatenating
  2885.        the argument names in reverse order
  2886.        with comma-space between them.  */
  2887.     defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
  2888.     {
  2889.       struct arglist *temp;
  2890.       int i = 0;
  2891.       for (temp = arg_ptrs; temp; temp = temp->next) {
  2892.     bcopy (temp->name, &defn->argnames[i], temp->length);
  2893.     i += temp->length;
  2894.     if (temp->next != 0) {
  2895.       defn->argnames[i++] = ',';
  2896.       defn->argnames[i++] = ' ';
  2897.     }
  2898.       }
  2899.       defn->argnames[i] = 0;
  2900.     }
  2901.   } else {
  2902.     /* simple expansion or empty definition; gobble it */
  2903.     if (is_hor_space[*bp])
  2904.       ++bp;        /* skip exactly one blank/tab char */
  2905.     /* now everything from bp before limit is the definition. */
  2906.     defn = collect_expansion (bp, limit, -1, 0);
  2907.     defn->argnames = (U_CHAR *) "";
  2908.   }
  2909.  
  2910.   hashcode = hashf (symname, sym_length, HASHSIZE);
  2911.  
  2912.   {
  2913.     HASHNODE *hp;
  2914.     if ((hp = lookup (symname, sym_length, hashcode)) != NULL) {
  2915.       if (hp->type != T_MACRO
  2916.       || compare_defs (defn, hp->value.defn)) {
  2917.     U_CHAR *msg;            /* what pain... */
  2918.     msg = (U_CHAR *) alloca (sym_length + 20);
  2919.     bcopy (symname, msg, sym_length);
  2920.     strcpy ((char *) (msg + sym_length), " redefined");
  2921.     warning (msg);
  2922.       }
  2923.       /* Replace the old definition.  */
  2924.       hp->type = T_MACRO;
  2925.       hp->value.defn = defn;
  2926.     } else
  2927.       install (symname, sym_length, T_MACRO, defn, hashcode);
  2928.   }
  2929.  
  2930.   return 0;
  2931.  
  2932. nope:
  2933.  
  2934.   return 1;
  2935. }
  2936.  
  2937. /*
  2938.  * return zero if two DEFINITIONs are isomorphic
  2939.  */
  2940. int
  2941. compare_defs (d1, d2)
  2942.      DEFINITION *d1, *d2;
  2943. {
  2944.   register struct reflist *a1, *a2;
  2945.   register U_CHAR *p1 = d1->expansion;
  2946.   register U_CHAR *p2 = d2->expansion;
  2947.   int first = 1;
  2948.  
  2949.   if (d1->nargs != d2->nargs)
  2950.     return 1;
  2951.   if (strcmp ((char *)d1->argnames, (char *)d2->argnames))
  2952.     return 1;
  2953.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  2954.        a1 = a1->next, a2 = a2->next) {
  2955.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  2956.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  2957.     || a1->argno != a2->argno
  2958.     || a1->stringify != a2->stringify
  2959.     || a1->raw_before != a2->raw_before
  2960.     || a1->raw_after != a2->raw_after)
  2961.       return 1;
  2962.     first = 0;
  2963.     p1 += a1->nchars;
  2964.     p2 += a2->nchars;
  2965.   }
  2966.   if (a1 != a2)
  2967.     return 1;
  2968.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  2969.              p2, d2->length - (p2 - d2->expansion), 1))
  2970.     return 1;
  2971.   return 0;
  2972. }
  2973.  
  2974. /* Return 1 if two parts of two macro definitions are effectively different.
  2975.    One of the parts starts at BEG1 and has LEN1 chars;
  2976.    the other has LEN2 chars at BEG2.
  2977.    Any sequence of whitespace matches any other sequence of whitespace.
  2978.    FIRST means these parts are the first of a macro definition;
  2979.     so ignore leading whitespace entirely.
  2980.    LAST means these parts are the last of a macro definition;
  2981.     so ignore trailing whitespace entirely.  */
  2982.  
  2983. comp_def_part (first, beg1, len1, beg2, len2, last)
  2984.      int first;
  2985.      U_CHAR *beg1, *beg2;
  2986.      int len1, len2;
  2987.      int last;
  2988. {
  2989.   register U_CHAR *end1 = beg1 + len1;
  2990.   register U_CHAR *end2 = beg2 + len2;
  2991.   if (first) {
  2992.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  2993.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  2994.   }
  2995.   if (last) {
  2996.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  2997.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  2998.   }
  2999.   while (beg1 != end1 && beg2 != end2) {
  3000.     if (is_space[*beg1] && is_space[*beg2]) {
  3001.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  3002.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  3003.     } else if (*beg1 == *beg2) {
  3004.       beg1++; beg2++;
  3005.     } else break;
  3006.   }
  3007.   return (beg1 != end1) || (beg2 != end2);
  3008. }
  3009.  
  3010. /* Read a replacement list for a macro with parameters.
  3011.    Build the DEFINITION structure.
  3012.    Reads characters of text starting at BUF until LIMIT.
  3013.    ARGLIST specifies the formal parameters to look for
  3014.    in the text of the definition; NARGS is the number of args
  3015.    in that list, or -1 for a macro name that wants no argument list.
  3016.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  3017.    and NAMELEN is its length in characters.
  3018.    
  3019. Note that comments and backslash-newlines have already been deleted
  3020. from the argument.  */
  3021.  
  3022. /* Leading and trailing Space, Tab, etc. are converted to markers
  3023.    Newline Space, Newline Tab, etc.
  3024.    Newline Space makes a space in the final output
  3025.    but is discarded if stringified.  (Newline Tab is similar but
  3026.    makes a Tab instead.)
  3027.  
  3028.    If there is no trailing whitespace, a Newline Space is added at the end
  3029.    to prevent concatenation that would be contrary to the standard.  */
  3030.  
  3031. DEFINITION *
  3032. collect_expansion (buf, end, nargs, arglist)
  3033.      U_CHAR *buf, *end;
  3034.      int nargs;
  3035.      struct arglist *arglist;
  3036. {
  3037.   DEFINITION *defn;
  3038.   register U_CHAR *p, *limit, *lastp, *exp_p;
  3039.   struct reflist *endpat = NULL;
  3040.   /* Pointer to first nonspace after last ## seen.  */
  3041.   U_CHAR *concat = 0;
  3042.   /* Pointer to first nonspace after last single-# seen.  */
  3043.   U_CHAR *stringify = 0;
  3044.   int maxsize;
  3045.   int expected_delimiter = '\0';
  3046.  
  3047.   /* Scan thru the replacement list, ignoring comments and quoted
  3048.      strings, picking up on the macro calls.  It does a linear search
  3049.      thru the arg list on every potential symbol.  Profiling might say
  3050.      that something smarter should happen. */
  3051.  
  3052.   if (end < buf)
  3053.     abort ();
  3054.  
  3055.   /* Find the beginning of the trailing whitespace.  */
  3056.   /* Find end of leading whitespace.  */
  3057.   limit = end;
  3058.   p = buf;
  3059.   while (p < limit && is_space[limit[-1]]) limit--;
  3060.   while (p < limit && is_space[*p]) p++;
  3061.  
  3062.   /* Allocate space for the text in the macro definition.
  3063.      Leading and trailing whitespace chars need 2 bytes each.
  3064.      Each other input char may or may not need 1 byte,
  3065.      so this is an upper bound.
  3066.      The extra 2 are for invented trailing newline-marker and final null.  */
  3067.   maxsize = (sizeof (DEFINITION)
  3068.          + 2 * (end - limit) + 2 * (p - buf)
  3069.          + (limit - p) + 3);
  3070.   defn = (DEFINITION *) xcalloc (1, maxsize);
  3071.  
  3072.   defn->nargs = nargs;
  3073.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  3074.   lastp = exp_p;
  3075.  
  3076.   p = buf;
  3077.  
  3078.   /* Convert leading whitespace to Newline-markers.  */
  3079.   while (p < limit && is_space[*p]) {
  3080.     *exp_p++ = '\n';
  3081.     *exp_p++ = *p++;
  3082.   }
  3083.  
  3084.   /* Process the main body of the definition.  */
  3085.   while (p < limit) {
  3086.     int skipped_arg = 0;
  3087.     register U_CHAR c = *p++;
  3088.  
  3089.     *exp_p++ = c;
  3090.  
  3091.     if (!traditional) {
  3092.       switch (c) {
  3093.       case '\'':
  3094.       case '\"':
  3095.     for (; p < limit && *p != c; p++) {
  3096.       *exp_p++ = *p;
  3097.       if (*p == '\\') {
  3098.         *exp_p++ = *++p;
  3099.       }
  3100.     }
  3101.     *exp_p++ = *p++;
  3102.     break;
  3103.  
  3104.     /* Special hack: if a \# is written in the #define
  3105.        include a # in the definition.  This is useless for C code
  3106.        but useful for preprocessing other things.  */
  3107.  
  3108.       case '\\':
  3109.     if (p < limit && *p == '#') {
  3110.       /* Pass through this # */
  3111.       exp_p--;
  3112.       *exp_p++ = *p++;
  3113.     } else if (p < limit) {
  3114.       /* Otherwise backslash goes through but makes next char ordinary.  */
  3115.       *exp_p++ = *p++;
  3116.     }
  3117.     break;
  3118.  
  3119.       case '#':
  3120.     if (p < limit && *p == '#') {
  3121.       /* ##: concatenate preceding and following tokens.  */
  3122.       /* Take out the first #, discard preceding whitespace.  */
  3123.       exp_p--;
  3124.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  3125.         --exp_p;
  3126.       /* Skip the second #.  */
  3127.       p++;
  3128.       /* Discard following whitespace.  */
  3129.       SKIP_WHITE_SPACE (p);
  3130.       concat = p;
  3131.     } else {
  3132.       /* Single #: stringify following argument ref.
  3133.          Don't leave the # in the expansion.  */
  3134.       exp_p--;
  3135.       SKIP_WHITE_SPACE (p);
  3136.       if (p == limit || ! is_idstart[*p] || nargs <= 0)
  3137.         error ("# operator should be followed by a macro argument name\n");
  3138.       else
  3139.         stringify = p;
  3140.     }
  3141.     break;
  3142.       }
  3143.     } else {
  3144.       /* In -traditional mode, recognize arguments inside strings and
  3145.      and character constants, and ignore special properties of #.
  3146.      Arguments inside strings are considered "stringified", but no
  3147.      extra quote marks are supplied.  */
  3148.       switch (c) {
  3149.       case '\'':
  3150.       case '\"':
  3151.     if (expected_delimiter != '\0') {
  3152.       if (c == expected_delimiter)
  3153.         expected_delimiter = '\0';
  3154.     } else
  3155.       expected_delimiter = c;
  3156.     break;
  3157.  
  3158.       case '\\':
  3159.     /* Backslash quotes delimiters and itself, but not macro args.  */
  3160.     if (expected_delimiter != 0 && p < limit
  3161.         && (*p == expected_delimiter || *p == '\\')) {
  3162.       *exp_p++ = *p++;
  3163.       continue;
  3164.     }
  3165.     break;
  3166.  
  3167.       case '/':
  3168.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  3169.       break;
  3170.     if (*p == '*') {
  3171.       /* If we find a comment that wasn't removed by handle_directive,
  3172.          this must be -traditional.  So replace the comment with
  3173.          nothing at all.  */
  3174.       exp_p--;
  3175.       p += 1;
  3176.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  3177.         p++;
  3178.       /* Mark this as a concatenation-point, as if it had been ##.  */
  3179.       concat = p;
  3180.     }
  3181.     break;
  3182.       }
  3183.     }
  3184.  
  3185.     if (is_idchar[c] && nargs > 0) {
  3186.       U_CHAR *id_beg = p - 1;
  3187.       int id_len;
  3188.  
  3189.       --exp_p;
  3190.       while (p != limit && is_idchar[*p]) p++;
  3191.       id_len = p - id_beg;
  3192.  
  3193.       if (is_idstart[c]) {
  3194.     register struct arglist *arg;
  3195.  
  3196.     for (arg = arglist; arg != NULL; arg = arg->next) {
  3197.       struct reflist *tpat;
  3198.  
  3199.       if (arg->name[0] == c
  3200.           && arg->length == id_len
  3201.           && strncmp (arg->name, id_beg, id_len) == 0) {
  3202.         /* make a pat node for this arg and append it to the end of
  3203.            the pat list */
  3204.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  3205.         tpat->next = NULL;
  3206.         tpat->raw_before = concat == id_beg;
  3207.         tpat->raw_after = 0;
  3208.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  3209.                    : stringify == id_beg);
  3210.  
  3211.         if (endpat == NULL)
  3212.           defn->pattern = tpat;
  3213.         else
  3214.           endpat->next = tpat;
  3215.         endpat = tpat;
  3216.  
  3217.         tpat->argno = arg->argno;
  3218.         tpat->nchars = exp_p - lastp;
  3219.         {
  3220.           register U_CHAR *p1 = p;
  3221.           SKIP_WHITE_SPACE (p1);
  3222.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  3223.         tpat->raw_after = 1;
  3224.         }
  3225.         lastp = exp_p;    /* place to start copying from next time */
  3226.         skipped_arg = 1;
  3227.         break;
  3228.       }
  3229.     }
  3230.       }
  3231.  
  3232.       /* If this was not a macro arg, copy it into the expansion.  */
  3233.       if (! skipped_arg) {
  3234.     register U_CHAR *lim1 = p;
  3235.     p = id_beg;
  3236.     while (p != lim1)
  3237.       *exp_p++ = *p++;
  3238.     if (stringify == id_beg)
  3239.       error ("# operator should be followed by a macro argument name\n");
  3240.       }
  3241.     }
  3242.   }
  3243.  
  3244.   if (limit < end) {
  3245.     /* Convert trailing whitespace to Newline-markers.  */
  3246.     while (limit < end && is_space[*limit]) {
  3247.       *exp_p++ = '\n';
  3248.       *exp_p++ = *limit++;
  3249.     }
  3250.   } else if (!traditional) {
  3251.     /* There is no trailing whitespace, so invent some.  */
  3252.     *exp_p++ = '\n';
  3253.     *exp_p++ = ' ';
  3254.   }
  3255.  
  3256.   *exp_p = '\0';
  3257.  
  3258.   defn->length = exp_p - defn->expansion;
  3259.  
  3260.   /* Crash now if we overrun the allocated size.  */
  3261.   if (defn->length + 1 > maxsize)
  3262.     abort ();
  3263.  
  3264. #if 0
  3265. /* This isn't worth the time it takes.  */
  3266.   /* give back excess storage */
  3267.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  3268. #endif
  3269.  
  3270.   return defn;
  3271. }
  3272.  
  3273. /*
  3274.  * interpret #line command.  Remembers previously seen fnames
  3275.  * in its very own hash table.
  3276.  */
  3277. #define FNAME_HASHSIZE 37
  3278.  
  3279. do_line (buf, limit, op, keyword)
  3280.      U_CHAR *buf, *limit;
  3281.      FILE_BUF *op;
  3282.      struct directive *keyword;
  3283. {
  3284.   register U_CHAR *bp;
  3285.   FILE_BUF *ip = &instack[indepth];
  3286.   FILE_BUF tem;
  3287.   int new_lineno;
  3288.   enum file_change_code file_change = same_file;
  3289.  
  3290.   /* Expand any macros.  */
  3291.   tem = expand_to_temp_buffer (buf, limit, 0);
  3292.  
  3293.   /* Point to macroexpanded line, which is null-terminated now.  */
  3294.   bp = tem.buf;
  3295.   SKIP_WHITE_SPACE (bp);
  3296.  
  3297.   if (!isdigit (*bp)) {
  3298.     error ("invalid format #line command");
  3299.     return;
  3300.   }
  3301.  
  3302.   /* The Newline at the end of this line remains to be processed.
  3303.      To put the next line at the specified line number,
  3304.      we must store a line number now that is one less.  */
  3305.   new_lineno = atoi (bp) - 1;
  3306.  
  3307.   /* skip over the line number.  */
  3308.   while (isdigit (*bp))
  3309.     bp++;
  3310.   if (*bp && !is_space[*bp]) {
  3311.     error ("invalid format #line command");
  3312.     return;
  3313.   }
  3314.     
  3315.   SKIP_WHITE_SPACE (bp);
  3316.  
  3317.   if (*bp == '\"') {
  3318.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  3319.     HASHNODE *hp, **hash_bucket;
  3320.     U_CHAR *fname;
  3321.     int fname_length;
  3322.  
  3323.     fname = ++bp;
  3324.  
  3325.     while (*bp && *bp != '\"')
  3326.       bp++;
  3327.     if (*bp != '\"') {
  3328.       error ("invalid format #line command");
  3329.       return;
  3330.     }
  3331.  
  3332.     fname_length = bp - fname;
  3333.  
  3334.     bp++;
  3335.     SKIP_WHITE_SPACE (bp);
  3336.     if (*bp) {
  3337.       if (*bp == '1')
  3338.     file_change = enter_file;
  3339.       else if (*bp == '2')
  3340.     file_change = leave_file;
  3341.       else {
  3342.     error ("invalid format #line command");
  3343.     return;
  3344.       }
  3345.  
  3346.       bp++;
  3347.       SKIP_WHITE_SPACE (bp);
  3348.       if (*bp) {
  3349.     error ("invalid format #line command");
  3350.     return;
  3351.       }
  3352.     }
  3353.  
  3354.     hash_bucket =
  3355.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  3356.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  3357.       if (hp->length == fname_length &&
  3358.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  3359.     ip->fname = hp->value.cpval;
  3360.     break;
  3361.       }
  3362.     if (hp == 0) {
  3363.       /* Didn't find it; cons up a new one.  */
  3364.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  3365.       hp->next = *hash_bucket;
  3366.       *hash_bucket = hp;
  3367.  
  3368.       hp->length = fname_length;
  3369.       ip->fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  3370.       bcopy (fname, hp->value.cpval, fname_length);
  3371.     }
  3372.   } else if (*bp) {
  3373.     error ("invalid format #line command");
  3374.     return;
  3375.   }
  3376.  
  3377.   ip->lineno = new_lineno;
  3378.   output_line_command (ip, op, 0, file_change);
  3379.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  3380. }
  3381.  
  3382. /*
  3383.  * remove all definitions of symbol from symbol table.
  3384.  * according to un*x /lib/cpp, it is not an error to undef
  3385.  * something that has no definitions, so it isn't one here either.
  3386.  */
  3387. do_undef (buf, limit, op, keyword)
  3388.      U_CHAR *buf, *limit;
  3389.      FILE_BUF *op;
  3390.      struct directive *keyword;
  3391. {
  3392.   HASHNODE *hp;
  3393.  
  3394.   SKIP_WHITE_SPACE (buf);
  3395.  
  3396.   while ((hp = lookup (buf, -1, -1)) != NULL) {
  3397.     if (hp->type != T_MACRO)
  3398.       error ("undefining `%s'", hp->name);
  3399.     delete_macro (hp);
  3400.   }
  3401. }
  3402.  
  3403. /*
  3404.  * Report a fatal error detected by the program we are processing.
  3405.  * Use the text of the line in the error message, then terminate.
  3406.  * (We use error() because it prints the filename & line#.)
  3407.  */
  3408. do_error (buf, limit, op, keyword)
  3409.      U_CHAR *buf, *limit;
  3410.      FILE_BUF *op;
  3411.      struct directive *keyword;
  3412. {
  3413.   int length = limit - buf;
  3414.   char *copy = (char *) xmalloc (length + 1);
  3415.   bcopy (buf, copy, length);
  3416.   copy[length] = 0;
  3417.   SKIP_WHITE_SPACE (copy);
  3418.   error ("#error %s", copy);
  3419.   exit (FATAL_EXIT_CODE);
  3420. }
  3421.  
  3422. /* Remember the name of the current file being read from so that we can
  3423.    avoid ever including it again.  */
  3424.  
  3425. do_once ()
  3426. {
  3427.   int i;
  3428.   FILE_BUF *ip = NULL;
  3429.  
  3430.   for (i = indepth; i >= 0; i--)
  3431.     if (instack[i].fname != NULL) {
  3432.       ip = &instack[i];
  3433.       break;
  3434.     }
  3435.  
  3436.   if (ip != NULL)
  3437.     {
  3438.       struct file_name_list *new;
  3439.  
  3440.       new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  3441.       new->next = dont_repeat_files;
  3442.       dont_repeat_files = new;
  3443.       new->fname = savestring (ip->fname);
  3444.     }
  3445. }
  3446.  
  3447. /* #pragma and its argument line have already been copied to the output file.
  3448.    Here just check for recognized pragmas.  */
  3449.  
  3450. do_pragma (buf, limit)
  3451.      U_CHAR *buf, *limit;
  3452. {
  3453.   while (*buf == ' ' || *buf == '\t')
  3454.     buf++;
  3455.   if (!strncmp (buf, "once", 4))
  3456.     do_once ();
  3457. }
  3458.  
  3459. #if 0
  3460. /* This was a fun hack, but #pragma seems to start to be useful.
  3461.    By failing to recognize it, we pass it through unchanged to cc1.  */
  3462.  
  3463. /*
  3464.  * the behavior of the #pragma directive is implementation defined.
  3465.  * this implementation defines it as follows.
  3466.  */
  3467. do_pragma ()
  3468. {
  3469.   close (0);
  3470.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  3471.     goto nope;
  3472.   close (1);
  3473.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  3474.     goto nope;
  3475.   execl ("/usr/games/hack", "#pragma", 0);
  3476.   execl ("/usr/games/rogue", "#pragma", 0);
  3477.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  3478.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  3479. nope:
  3480.   fatal ("You are in a maze of twisty compiler features, all different");
  3481. }
  3482. #endif
  3483.  
  3484. /* Just ignore #sccs, on systems where we define it at all.  */
  3485. do_sccs ()
  3486. {
  3487.   if (pedantic)
  3488.     error ("ANSI C does not allow #sccs");
  3489. }
  3490.  
  3491. /*
  3492.  * handle #if command by
  3493.  *   1) inserting special `defined' keyword into the hash table
  3494.  *    that gets turned into 0 or 1 by special_symbol (thus,
  3495.  *    if the luser has a symbol called `defined' already, it won't
  3496.  *      work inside the #if command)
  3497.  *   2) rescan the input into a temporary output buffer
  3498.  *   3) pass the output buffer to the yacc parser and collect a value
  3499.  *   4) clean up the mess left from steps 1 and 2.
  3500.  *   5) call conditional_skip to skip til the next #endif (etc.),
  3501.  *      or not, depending on the value from step 3.
  3502.  */
  3503.  
  3504. do_if (buf, limit, op, keyword)
  3505.      U_CHAR *buf, *limit;
  3506.      FILE_BUF *op;
  3507.      struct directive *keyword;
  3508. {
  3509.   int value;
  3510.   FILE_BUF *ip = &instack[indepth];
  3511.  
  3512.   value = eval_if_expression (buf, limit - buf);
  3513.   conditional_skip (ip, value == 0, T_IF);
  3514. }
  3515.  
  3516. /*
  3517.  * handle a #elif directive by not changing  if_stack  either.
  3518.  * see the comment above do_else.
  3519.  */
  3520.  
  3521. do_elif (buf, limit, op, keyword)
  3522.      U_CHAR *buf, *limit;
  3523.      FILE_BUF *op;
  3524.      struct directive *keyword;
  3525. {
  3526.   int value;
  3527.   FILE_BUF *ip = &instack[indepth];
  3528.  
  3529.   if (if_stack == instack[indepth].if_stack) {
  3530.     error ("#elif not within a conditional");
  3531.     return;
  3532.   } else {
  3533.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3534.       error ("#elif after #else");
  3535.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3536.       if (if_stack->fname != NULL && ip->fname != NULL &&
  3537.       strcmp (if_stack->fname, ip->fname) != 0)
  3538.     fprintf (stderr, ", file %s", if_stack->fname);
  3539.       fprintf (stderr, ")\n");
  3540.     }
  3541.     if_stack->type = T_ELIF;
  3542.   }
  3543.  
  3544.   if (if_stack->if_succeeded)
  3545.     skip_if_group (ip, 0);
  3546.   else {
  3547.     value = eval_if_expression (buf, limit - buf);
  3548.     if (value == 0)
  3549.       skip_if_group (ip, 0);
  3550.     else {
  3551.       ++if_stack->if_succeeded;    /* continue processing input */
  3552.       output_line_command (ip, op, 1, same_file);
  3553.     }
  3554.   }
  3555. }
  3556.  
  3557. /*
  3558.  * evaluate a #if expression in BUF, of length LENGTH,
  3559.  * then parse the result as a C expression and return the value as an int.
  3560.  */
  3561. int
  3562. eval_if_expression (buf, length)
  3563.      U_CHAR *buf;
  3564.      int length;
  3565. {
  3566.   FILE_BUF temp_obuf;
  3567.   HASHNODE *save_defined;
  3568.   int value;
  3569.  
  3570.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
  3571.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
  3572.   delete_macro (save_defined);    /* clean up special symbol */
  3573.  
  3574.   value = parse_c_expression (temp_obuf.buf);
  3575.  
  3576.   free (temp_obuf.buf);
  3577.  
  3578.   return value;
  3579. }
  3580.  
  3581. /*
  3582.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  3583.  * then do or don't skip to the #endif/#else/#elif depending
  3584.  * on what directive is actually being processed.
  3585.  */
  3586. do_xifdef (buf, limit, op, keyword)
  3587.      U_CHAR *buf, *limit;
  3588.      FILE_BUF *op;
  3589.      struct directive *keyword;
  3590. {
  3591.   int skip;
  3592.   FILE_BUF *ip = &instack[indepth];
  3593.   U_CHAR *end; 
  3594.  
  3595.   /* Discard leading and trailing whitespace.  */
  3596.   SKIP_WHITE_SPACE (buf);
  3597.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  3598.  
  3599.   /* Find the end of the identifier at the beginning.  */
  3600.   for (end = buf; is_idchar[*end]; end++);
  3601.  
  3602.   if (end == buf) {
  3603.     skip = (keyword->type == T_IFDEF);
  3604.     if (! traditional)
  3605.       warning (end == limit ? "#%s with no argument"
  3606.            : "#%s argument starts with punctuation",
  3607.            keyword->name);
  3608.   } else {
  3609.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  3610.       warning ("#%s argument starts with a digit", keyword->name);
  3611.     else if (end != limit && !traditional)
  3612.       warning ("garbage at end of #%s argument", keyword->name);
  3613.  
  3614.     skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
  3615.   }
  3616.  
  3617.   conditional_skip (ip, skip, T_IF);
  3618. }
  3619.  
  3620. /*
  3621.  * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  3622.  */
  3623. void
  3624. conditional_skip (ip, skip, type)
  3625.      FILE_BUF *ip;
  3626.      int skip;
  3627.      enum node_type type;
  3628. {
  3629.   IF_STACK_FRAME *temp;
  3630.  
  3631.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3632.   temp->fname = ip->fname;
  3633.   temp->lineno = ip->lineno;
  3634.   temp->next = if_stack;
  3635.   if_stack = temp;
  3636.  
  3637.   if_stack->type = type;
  3638.  
  3639.   if (skip != 0) {
  3640.     skip_if_group (ip, 0);
  3641.     return;
  3642.   } else {
  3643.     ++if_stack->if_succeeded;
  3644.     output_line_command (ip, &outbuf, 1, same_file);
  3645.   }
  3646. }
  3647.  
  3648. /*
  3649.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  3650.  * leaves input ptr at the sharp sign found.
  3651.  * If ANY is nonzero, return at next directive of any sort.
  3652.  */
  3653. void
  3654. skip_if_group (ip, any)
  3655.      FILE_BUF *ip;
  3656.      int any;
  3657. {
  3658.   register U_CHAR *bp = ip->bufp, *cp;
  3659.   register U_CHAR *endb = ip->buf + ip->length;
  3660.   struct directive *kt;
  3661.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  3662.   U_CHAR *beg_of_line = bp;
  3663.  
  3664.   while (bp < endb) {
  3665.     switch (*bp++) {
  3666.     case '/':            /* possible comment */
  3667.       if (*bp == '\\' && bp[1] == '\n')
  3668.     newline_fix (bp);
  3669.       if (*bp == '*'
  3670.       || (cplusplus && *bp == '/')) {
  3671.     ip->bufp = ++bp;
  3672.     bp = skip_to_end_of_comment (ip, &ip->lineno);
  3673.       }
  3674.       break;
  3675.     case '\"':
  3676.     case '\'':
  3677.       if (!traditional)
  3678.     bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
  3679.       break;
  3680.     case '\\':
  3681.       /* Char after backslash loses its special meaning.  */
  3682.       if (bp < endb) {
  3683.     if (*bp == '\n')
  3684.       ++ip->lineno;        /* But do update the line-count.  */
  3685.     bp++;
  3686.       }
  3687.       break;
  3688.     case '\n':
  3689.       ++ip->lineno;
  3690.       beg_of_line = bp;
  3691.       break;
  3692.     case '#':
  3693.       ip->bufp = bp - 1;
  3694.  
  3695.       /* # keyword: a # must be first nonblank char on the line */
  3696.       if (beg_of_line == 0)
  3697.     break;
  3698.       /* Scan from start of line, skipping whitespace, comments
  3699.      and backslash-newlines, and see if we reach this #.
  3700.      If not, this # is not special.  */
  3701.       bp = beg_of_line;
  3702.       while (1) {
  3703.     if (is_hor_space[*bp])
  3704.       bp++;
  3705.     else if (*bp == '\\' && bp[1] == '\n')
  3706.       bp += 2;
  3707.     else if (*bp == '/' && bp[1] == '*') {
  3708.       bp += 2;
  3709.       while (!(*bp == '*' && bp[1] == '/'))
  3710.         bp++;
  3711.       bp += 2;
  3712.     }
  3713.     else if (cplusplus && *bp == '/' && bp[1] == '/') {
  3714.       bp += 2;
  3715.       while (*bp++ != '\n') ;
  3716.         }
  3717.     else break;
  3718.       }
  3719.       if (bp != ip->bufp) {
  3720.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  3721.     break;
  3722.       }
  3723.  
  3724.       bp = ip->bufp + 1;        /* point at '#' */
  3725.  
  3726.       /* Skip whitespace and \-newline.  */
  3727.       while (1) {
  3728.     if (is_hor_space[*bp])
  3729.       bp++;
  3730.     else if (*bp == '\\' && bp[1] == '\n')
  3731.       bp += 2;
  3732.     else break;
  3733.       }
  3734.  
  3735.       cp = bp;
  3736.  
  3737.       /* Now find end of directive name.
  3738.      If we encounter a backslash-newline, exchange it with any following
  3739.      symbol-constituents so that we end up with a contiguous name.  */
  3740.  
  3741.       while (1) {
  3742.     if (is_idchar[*bp])
  3743.       bp++;
  3744.     else {
  3745.       if (*bp == '\\' && bp[1] == '\n')
  3746.         name_newline_fix (bp);
  3747.       if (is_idchar[*bp])
  3748.         bp++;
  3749.       else break;
  3750.     }
  3751.       }
  3752.  
  3753.       for (kt = directive_table; kt->length >= 0; kt++) {
  3754.     IF_STACK_FRAME *temp;
  3755.     if (strncmp (cp, kt->name, kt->length) == 0
  3756.         && !is_idchar[cp[kt->length]]) {
  3757.  
  3758.       /* If we are asked to return on next directive,
  3759.          do so now.  */
  3760.       if (any)
  3761.         return;
  3762.  
  3763.       switch (kt->type) {
  3764.       case T_IF:
  3765.       case T_IFDEF:
  3766.       case T_IFNDEF:
  3767.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3768.         temp->next = if_stack;
  3769.         if_stack = temp;
  3770.         temp->lineno = ip->lineno;
  3771.         temp->fname = ip->fname;
  3772.         temp->type = kt->type;
  3773.         break;
  3774.       case T_ELSE:
  3775.       case T_ENDIF:
  3776.         if (pedantic && if_stack != save_if_stack)
  3777.           validate_else (bp);
  3778.       case T_ELIF:
  3779.         if (if_stack == instack[indepth].if_stack) {
  3780.           error ("#%s not within a conditional", kt->name);
  3781.           break;
  3782.         }
  3783.         else if (if_stack == save_if_stack)
  3784.           return;        /* found what we came for */
  3785.  
  3786.         if (kt->type != T_ENDIF) {
  3787.           if (if_stack->type == T_ELSE)
  3788.         error ("#else or #elif after #else");
  3789.           if_stack->type = kt->type;
  3790.           break;
  3791.         }
  3792.  
  3793.         temp = if_stack;
  3794.         if_stack = if_stack->next;
  3795.         free (temp);
  3796.         break;
  3797.       }
  3798.       break;
  3799.     }
  3800.       }
  3801.     }
  3802.   }
  3803.   ip->bufp = bp;
  3804.   /* after this returns, rescan will exit because ip->bufp
  3805.      now points to the end of the buffer.
  3806.      rescan is responsible for the error message also.  */
  3807. }
  3808.  
  3809. /*
  3810.  * handle a #else directive.  Do this by just continuing processing
  3811.  * without changing  if_stack ;  this is so that the error message
  3812.  * for missing #endif's etc. will point to the original #if.  It
  3813.  * is possible that something different would be better.
  3814.  */
  3815. do_else (buf, limit, op, keyword)
  3816.      U_CHAR *buf, *limit;
  3817.      FILE_BUF *op;
  3818.      struct directive *keyword;
  3819. {
  3820.   FILE_BUF *ip = &instack[indepth];
  3821.  
  3822.   if (pedantic) {
  3823.     SKIP_WHITE_SPACE (buf);
  3824.     if (buf != limit)
  3825.       warning ("text following #else violates ANSI standard");
  3826.   }
  3827.  
  3828.   if (if_stack == instack[indepth].if_stack) {
  3829.     error ("#else not within a conditional");
  3830.     return;
  3831.   } else {
  3832.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3833.       error ("#else after #else");
  3834.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3835.       if (strcmp (if_stack->fname, ip->fname) != 0)
  3836.     fprintf (stderr, ", file %s", if_stack->fname);
  3837.       fprintf (stderr, ")\n");
  3838.     }
  3839.     if_stack->type = T_ELSE;
  3840.   }
  3841.  
  3842.   if (if_stack->if_succeeded)
  3843.     skip_if_group (ip, 0);
  3844.   else {
  3845.     ++if_stack->if_succeeded;    /* continue processing input */
  3846.     output_line_command (ip, op, 1, same_file);
  3847.   }
  3848. }
  3849.  
  3850. /*
  3851.  * unstack after #endif command
  3852.  */
  3853. do_endif (buf, limit, op, keyword)
  3854.      U_CHAR *buf, *limit;
  3855.      FILE_BUF *op;
  3856.      struct directive *keyword;
  3857. {
  3858.   if (pedantic) {
  3859.     SKIP_WHITE_SPACE (buf);
  3860.     if (buf != limit)
  3861.       warning ("text following #endif violates ANSI standard");
  3862.   }
  3863.  
  3864.   if (if_stack == instack[indepth].if_stack)
  3865.     error ("unbalanced #endif");
  3866.   else {
  3867.     IF_STACK_FRAME *temp = if_stack;
  3868.     if_stack = if_stack->next;
  3869.     free (temp);
  3870.     output_line_command (&instack[indepth], op, 1, same_file);
  3871.   }
  3872. }
  3873.  
  3874. /* When an #else or #endif is found while skipping failed conditional,
  3875.    if -pedantic was specified, this is called to warn about text after
  3876.    the command name.  P points to the first char after the command name.  */
  3877.  
  3878. validate_else (p)
  3879.      register U_CHAR *p;
  3880. {
  3881.   /* Advance P over whitespace and comments.  */
  3882.   while (1) {
  3883.     if (*p == '\\' && p[1] == '\n')
  3884.       p += 2;
  3885.     if (is_hor_space[*p])
  3886.       p++;
  3887.     else if (*p == '/') {
  3888.       if (p[1] == '\\' && p[2] == '\n')
  3889.     newline_fix (p + 1);
  3890.       if (p[1] == '*') {
  3891.     p += 2;
  3892.     /* Don't bother warning about unterminated comments
  3893.        since that will happen later.  Just be sure to exit.  */
  3894.     while (*p) {
  3895.       if (p[1] == '\\' && p[2] == '\n')
  3896.         newline_fix (p + 1);
  3897.       if (*p == '*' && p[1] == '/') {
  3898.         p += 2;
  3899.         break;
  3900.       }
  3901.       p++;
  3902.     }
  3903.       }
  3904.       else if (cplusplus && p[1] == '/') {
  3905.     p += 2;
  3906.     while (*p && *p++ != '\n') ;
  3907.       }
  3908.     } else break;
  3909.   }
  3910.   if (*p && *p != '\n')
  3911.     warning ("text following #else or #endif violates ANSI standard");
  3912. }
  3913.  
  3914. /*
  3915.  * Skip a comment, assuming the input ptr immediately follows the
  3916.  * initial slash-star.  Bump line counter as necessary.
  3917.  * (The canonical line counter is &ip->lineno).
  3918.  * Don't use this routine (or the next one) if bumping the line
  3919.  * counter is not sufficient to deal with newlines in the string.
  3920.  */
  3921. U_CHAR *
  3922. skip_to_end_of_comment (ip, line_counter)
  3923.      register FILE_BUF *ip;
  3924.      int *line_counter;        /* place to remember newlines, or NULL */
  3925. {
  3926.   register U_CHAR *limit = ip->buf + ip->length;
  3927.   register U_CHAR *bp = ip->bufp;
  3928.   FILE_BUF *op = &outbuf;    /* JF */
  3929.   int output = put_out_comments && !line_counter;
  3930.  
  3931.     /* JF this line_counter stuff is a crock to make sure the
  3932.        comment is only put out once, no matter how many times
  3933.        the comment is skipped.  It almost works */
  3934.   if (output) {
  3935.     *op->bufp++ = '/';
  3936.     *op->bufp++ = '*';
  3937.   }
  3938.   if (cplusplus && bp[-1] == '/') {
  3939.     if (output) {
  3940.       while (bp < limit)
  3941.     if ((*op->bufp++ = *bp++) == '\n') {
  3942.       bp--;
  3943.       break;
  3944.     }
  3945.       op->bufp[-1] = '*';
  3946.       *op->bufp++ = '/';
  3947.       *op->bufp++ = '\n';
  3948.     } else {
  3949.       while (bp < limit) {
  3950.     if (*bp++ == '\n') {
  3951.       bp--;
  3952.       break;
  3953.     }
  3954.       }
  3955.     }
  3956.     ip->bufp = bp;
  3957.     return bp;
  3958.   }
  3959.   while (bp < limit) {
  3960.     if (output)
  3961.       *op->bufp++ = *bp;
  3962.     switch (*bp++) {
  3963.     case '\n':
  3964.       if (line_counter != NULL)
  3965.     ++*line_counter;
  3966.       if (output)
  3967.     ++op->lineno;
  3968.       break;
  3969.     case '*':
  3970.       if (*bp == '\\' && bp[1] == '\n')
  3971.     newline_fix (bp);
  3972.       if (*bp == '/') {
  3973.         if (output)
  3974.       *op->bufp++ = '/';
  3975.     ip->bufp = ++bp;
  3976.     return bp;
  3977.       }
  3978.       break;
  3979.     }
  3980.   }
  3981.   ip->bufp = bp;
  3982.   return bp;
  3983. }
  3984.  
  3985. /*
  3986.  * Skip over a quoted string.  BP points to the opening quote.
  3987.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  3988.  * START_LINE is the line number of the starting point (but it need
  3989.  * not be valid if the starting point is inside a macro expansion).
  3990.  *
  3991.  * The input stack state is not changed.
  3992.  *
  3993.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  3994.  * for each newline passed.
  3995.  *
  3996.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  3997.  * if we pass a backslash-newline.
  3998.  *
  3999.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  4000.  */
  4001. U_CHAR *
  4002. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  4003.      register U_CHAR *bp;
  4004.      register U_CHAR *limit;
  4005.      int start_line;
  4006.      int *count_newlines;
  4007.      int *backslash_newlines_p;
  4008.      int *eofp;
  4009. {
  4010.   register U_CHAR c, match;
  4011.  
  4012.   match = *bp++;
  4013.   while (1) {
  4014.     if (bp >= limit) {
  4015.       error_with_line (line_for_error (start_line),
  4016.                "unterminated string or character constant");
  4017.       if (eofp)
  4018.     *eofp = 1;
  4019.       break;
  4020.     }
  4021.     c = *bp++;
  4022.     if (c == '\\') {
  4023.       while (*bp == '\\' && bp[1] == '\n') {
  4024.     if (backslash_newlines_p)
  4025.       *backslash_newlines_p = 1;
  4026.     if (count_newlines)
  4027.       ++*count_newlines;
  4028.     bp += 2;
  4029.       }
  4030.       if (*bp == '\n' && count_newlines) {
  4031.     if (backslash_newlines_p)
  4032.       *backslash_newlines_p = 1;
  4033.     ++*count_newlines;
  4034.       }
  4035.       bp++;
  4036.     } else if (c == '\n') {
  4037.       if (match == '\'') {
  4038.     error_with_line (line_for_error (start_line),
  4039.              "unterminated character constant");
  4040.     bp--;
  4041.     if (eofp)
  4042.       *eofp = 1;
  4043.     break;
  4044.       }
  4045.       if (traditional) {    /* Unterminated strings are 'legal'.  */
  4046.     if (eofp)
  4047.       *eofp = 1;
  4048.     break;
  4049.       }
  4050.       if (count_newlines)
  4051.     ++*count_newlines;
  4052.     } else if (c == match)
  4053.       break;
  4054.   }
  4055.   return bp;
  4056. }
  4057.  
  4058. /*
  4059.  * write out a #line command, for instance, after an #include file.
  4060.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  4061.  * appear to be a no-op, and we can output a few newlines instead
  4062.  * if we want to increase the line number by a small amount.
  4063.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  4064.  */
  4065.  
  4066. void
  4067. output_line_command (ip, op, conditional, file_change)
  4068.      FILE_BUF *ip, *op;
  4069.      int conditional;
  4070.      enum file_change_code file_change;
  4071. {
  4072.   int len;
  4073.   char line_cmd_buf[500];
  4074.  
  4075.   if (no_line_commands
  4076.       || ip->fname == NULL
  4077.       || no_output) {
  4078.     op->lineno = ip->lineno;
  4079.     return;
  4080.   }
  4081.  
  4082.   if (conditional) {
  4083.     if (ip->lineno == op->lineno)
  4084.       return;
  4085.  
  4086.     /* If the inherited line number is a little too small,
  4087.        output some newlines instead of a #line command.  */
  4088.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  4089.       check_expand (op, 10);
  4090.       while (ip->lineno > op->lineno) {
  4091.     *op->bufp++ = '\n';
  4092.     op->lineno++;
  4093.       }
  4094.       return;
  4095.     }
  4096.   }
  4097.  
  4098. #ifdef OUTPUT_LINE_COMMANDS
  4099.   sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->fname);
  4100. #else
  4101.   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
  4102. #endif
  4103.   if (file_change != same_file)
  4104.     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
  4105.   len = strlen (line_cmd_buf);
  4106.   line_cmd_buf[len++] = '\n';
  4107.   check_expand (op, len + 1);
  4108.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  4109.     *op->bufp++ = '\n';
  4110.   bcopy (line_cmd_buf, op->bufp, len);
  4111.   op->bufp += len;
  4112.   op->lineno = ip->lineno;
  4113. }
  4114.  
  4115. /* This structure represents one parsed argument in a macro call.
  4116.    `raw' points to the argument text as written (`raw_length' is its length).
  4117.    `expanded' points to the argument's macro-expansion
  4118.    (its length is `expand_length').
  4119.    `stringified_length' is the length the argument would have
  4120.    if stringified.
  4121.    `free1' and `free2', if nonzero, point to blocks to be freed
  4122.    when the macro argument data is no longer needed.  */
  4123.  
  4124. struct argdata {
  4125.   U_CHAR *raw, *expanded;
  4126.   int raw_length, expand_length;
  4127.   int stringified_length;
  4128.   U_CHAR *free1, *free2;
  4129.   char newlines;
  4130.   char comments;
  4131. };
  4132.  
  4133. /* Expand a macro call.
  4134.    HP points to the symbol that is the macro being called.
  4135.    Put the result of expansion onto the input stack
  4136.    so that subsequent input by our caller will use it.
  4137.  
  4138.    If macro wants arguments, caller has already verified that
  4139.    an argument list follows; arguments come from the input stack.  */
  4140.  
  4141. void
  4142. macroexpand (hp, op)
  4143.      HASHNODE *hp;
  4144.      FILE_BUF *op;
  4145. {
  4146.   int nargs;
  4147.   DEFINITION *defn = hp->value.defn;
  4148.   register U_CHAR *xbuf;
  4149.   int xbuf_len;
  4150.   int start_line = instack[indepth].lineno;
  4151.  
  4152.   CHECK_DEPTH (return;);
  4153.  
  4154.   /* it might not actually be a macro.  */
  4155.   if (hp->type != T_MACRO) {
  4156.     special_symbol (hp, op);
  4157.     return;
  4158.   }
  4159.  
  4160.   nargs = defn->nargs;
  4161.  
  4162.   if (nargs >= 0) {
  4163.     register int i;
  4164.     struct argdata *args;
  4165.     char *parse_error = 0;
  4166.  
  4167.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  4168.  
  4169.     for (i = 0; i < nargs; i++) {
  4170.       args[i].raw = args[i].expanded = (U_CHAR *) "";
  4171.       args[i].raw_length = args[i].expand_length
  4172.     = args[i].stringified_length = 0;
  4173.       args[i].free1 = args[i].free2 = 0;
  4174.     }
  4175.  
  4176.     /* Parse all the macro args that are supplied.  I counts them.
  4177.        The first NARGS args are stored in ARGS.
  4178.        The rest are discarded.  */
  4179.     i = 0;
  4180.     do {
  4181.       /* Discard the open-parenthesis or comma before the next arg.  */
  4182.       ++instack[indepth].bufp;
  4183.       parse_error
  4184.     = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
  4185.       if (parse_error)
  4186.     {
  4187.       error_with_line (line_for_error (start_line), parse_error);
  4188.       break;
  4189.     }
  4190.       i++;
  4191.     } while (*instack[indepth].bufp != ')');
  4192.  
  4193.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  4194.     if (i == 1) {
  4195.       register U_CHAR *bp = args[0].raw;
  4196.       register U_CHAR *lim = bp + args[0].raw_length;
  4197.       while (bp != lim && is_space[*bp]) bp++;
  4198.       if (bp == lim)
  4199.     i = 0;
  4200.     }
  4201.  
  4202.     if (nargs == 0 && i > 0)
  4203.       error ("arguments given to macro `%s'", hp->name);
  4204.     else if (i < nargs) {
  4205.       /* traditional C allows foo() if foo wants one argument.  */
  4206.       if (nargs == 1 && i == 0 && traditional)
  4207.     ;
  4208.       else if (i == 0)
  4209.     error ("no args to macro `%s'", hp->name);
  4210.       else if (i == 1)
  4211.     error ("only 1 arg to macro `%s'", hp->name);
  4212.       else
  4213.     error ("only %d args to macro `%s'", i, hp->name);
  4214.     } else if (i > nargs)
  4215.       error ("too many (%d) args to macro `%s'", i, hp->name);
  4216.  
  4217.     /* Swallow the closeparen.  */
  4218.     ++instack[indepth].bufp;
  4219.  
  4220.     /* If macro wants zero args, we parsed the arglist for checking only.
  4221.        Read directly from the macro definition.  */
  4222.     if (nargs == 0) {
  4223.       xbuf = defn->expansion;
  4224.       xbuf_len = defn->length;
  4225.     } else {
  4226.       register U_CHAR *exp = defn->expansion;
  4227.       register int offset;    /* offset in expansion,
  4228.                    copied a piece at a time */
  4229.       register int totlen;    /* total amount of exp buffer filled so far */
  4230.  
  4231.       register struct reflist *ap;
  4232.  
  4233.       /* Macro really takes args.  Compute the expansion of this call.  */
  4234.  
  4235.       /* Compute length in characters of the macro's expansion.  */
  4236.       xbuf_len = defn->length;
  4237.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4238.     if (ap->stringify)
  4239.       xbuf_len += args[ap->argno].stringified_length;
  4240.     else if (ap->raw_before || ap->raw_after)
  4241.       xbuf_len += args[ap->argno].raw_length;
  4242.     else
  4243.       xbuf_len += args[ap->argno].expand_length;
  4244.       }
  4245.  
  4246.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  4247.  
  4248.       /* Generate in XBUF the complete expansion
  4249.      with arguments substituted in.
  4250.      TOTLEN is the total size generated so far.
  4251.      OFFSET is the index in the definition
  4252.      of where we are copying from.  */
  4253.       offset = totlen = 0;
  4254.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4255.     register struct argdata *arg = &args[ap->argno];
  4256.  
  4257.     for (i = 0; i < ap->nchars; i++)
  4258.       xbuf[totlen++] = exp[offset++];
  4259.  
  4260.     if (ap->stringify != 0) {
  4261.       int arglen = arg->raw_length;
  4262.       int escaped = 0;
  4263.       int in_string = 0;
  4264.       int c;
  4265.       i = 0;
  4266.       while (i < arglen
  4267.          && (c = arg->raw[i], is_space[c]))
  4268.         i++;
  4269.       while (i < arglen
  4270.          && (c = arg->raw[arglen - 1], is_space[c]))
  4271.         arglen--;
  4272.       if (!traditional)
  4273.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  4274.       for (; i < arglen; i++) {
  4275.         c = arg->raw[i];
  4276.  
  4277.         /* Special markers Newline Space
  4278.            generate nothing for a stringified argument.  */
  4279.         if (c == '\n' && arg->raw[i+1] != '\n') {
  4280.           i++;
  4281.           continue;
  4282.         }
  4283.  
  4284.         /* Internal sequences of whitespace are replaced by one space.  */
  4285.         if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
  4286.           while (1) {
  4287.         if (c == '\n' && arg->raw[i+1] == '\n')
  4288.           i += 2;
  4289.         else if (c != '\n' && is_space[c])
  4290.           i++;
  4291.         else break;
  4292.         c = arg->raw[i];
  4293.           }
  4294.           i--;
  4295.           c = ' ';
  4296.         }
  4297.  
  4298.         if (escaped)
  4299.           escaped = 0;
  4300.         else {
  4301.           if (c == '\\')
  4302.         escaped = 1;
  4303.           if (in_string && c == in_string)
  4304.         in_string = 0;
  4305.           else if (c == '\"' || c == '\'')
  4306.         in_string = c;
  4307.         }
  4308.  
  4309.         /* Escape these chars */
  4310.         if (c == '\"' || (in_string && c == '\\'))
  4311.           xbuf[totlen++] = '\\';
  4312.         if (isprint (c))
  4313.           xbuf[totlen++] = c;
  4314.         else {
  4315.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  4316.           totlen += 4;
  4317.         }
  4318.       }
  4319.       if (!traditional)
  4320.         xbuf[totlen++] = '\"'; /* insert ending quote */
  4321.     } else if (ap->raw_before || ap->raw_after) {
  4322.       U_CHAR *p1 = arg->raw;
  4323.       U_CHAR *l1 = p1 + arg->raw_length;
  4324.       if (ap->raw_before) {
  4325.         while (p1 != l1 && is_space[*p1]) p1++;
  4326.         while (p1 != l1 && is_idchar[*p1])
  4327.           xbuf[totlen++] = *p1++;
  4328.         /* Delete any no-reexpansion marker that follows
  4329.            an identifier at the beginning of the argument
  4330.            if the argument is concatenated with what precedes it.  */
  4331.         if (p1[0] == '\n' && p1[1] == '-')
  4332.           p1 += 2;
  4333.       }
  4334.       if (ap->raw_after) {
  4335.         /* Arg is concatenated after: delete trailing whitespace,
  4336.            whitespace markers, and no-reexpansion markers.  */
  4337.         while (p1 != l1) {
  4338.           if (is_space[l1[-1]]) l1--;
  4339.           else if (l1[-1] == '-') {
  4340.         U_CHAR *p2 = l1 - 1;
  4341.         /* If a `-' is preceded by an odd number of newlines then it
  4342.            and the last newline are a no-reexpansion marker.  */
  4343.         while (p2 != p1 && p2[-1] == '\n') p2--;
  4344.         if ((l1 - 1 - p2) & 1) {
  4345.           l1 -= 2;
  4346.         }
  4347.         else break;
  4348.           }
  4349.           else break;
  4350.         }
  4351.       }
  4352.       bcopy (p1, xbuf + totlen, l1 - p1);
  4353.       totlen += l1 - p1;
  4354.     } else {
  4355.       bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  4356.       totlen += arg->expand_length;
  4357.     }
  4358.  
  4359.     if (totlen > xbuf_len)
  4360.       abort ();
  4361.       }
  4362.  
  4363.       /* if there is anything left of the definition
  4364.      after handling the arg list, copy that in too. */
  4365.  
  4366.       for (i = offset; i < defn->length; i++)
  4367.     xbuf[totlen++] = exp[i];
  4368.  
  4369.       xbuf[totlen] = 0;
  4370.       xbuf_len = totlen;
  4371.  
  4372.       for (i = 0; i < nargs; i++) {
  4373.     if (args[i].free1 != 0)
  4374.       free (args[i].free1);
  4375.     if (args[i].free2 != 0)
  4376.       free (args[i].free2);
  4377.       }
  4378.     }
  4379.   } else {
  4380.     xbuf = defn->expansion;
  4381.     xbuf_len = defn->length;
  4382.   }
  4383.  
  4384.   /* Now put the expansion on the input stack
  4385.      so our caller will commence reading from it.  */
  4386.   {
  4387.     register FILE_BUF *ip2;
  4388.  
  4389.     ip2 = &instack[++indepth];
  4390.  
  4391.     ip2->fname = 0;
  4392.     ip2->lineno = 0;
  4393.     ip2->buf = xbuf;
  4394.     ip2->length = xbuf_len;
  4395.     ip2->bufp = xbuf;
  4396.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  4397.     ip2->macro = hp;
  4398.     ip2->if_stack = if_stack;
  4399.  
  4400.     /* Recursive macro use sometimes works traditionally.
  4401.        #define foo(x,y) bar(x(y,0), y)
  4402.        foo(foo, baz)  */
  4403.  
  4404.     if (!traditional)
  4405.       hp->type = T_DISABLED;
  4406.   }
  4407. }
  4408.  
  4409. /*
  4410.  * Parse a macro argument and store the info on it into *ARGPTR.
  4411.  * Return nonzero to indicate a syntax error.
  4412.  */
  4413.  
  4414. char *
  4415. macarg (argptr)
  4416.      register struct argdata *argptr;
  4417. {
  4418.   FILE_BUF *ip = &instack[indepth];
  4419.   int paren = 0;
  4420.   int newlines = 0;
  4421.   int comments = 0;
  4422.  
  4423.   /* Try to parse as much of the argument as exists at this
  4424.      input stack level.  */
  4425.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  4426.             &paren, &newlines, &comments);
  4427.  
  4428.   /* If we find the end of the argument at this level,
  4429.      set up *ARGPTR to point at it in the input stack.  */
  4430.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  4431.       && bp != ip->buf + ip->length) {
  4432.     if (argptr != 0) {
  4433.       argptr->raw = ip->bufp;
  4434.       argptr->raw_length = bp - ip->bufp;
  4435.     }
  4436.     ip->bufp = bp;
  4437.   } else {
  4438.     /* This input stack level ends before the macro argument does.
  4439.        We must pop levels and keep parsing.
  4440.        Therefore, we must allocate a temporary buffer and copy
  4441.        the macro argument into it.  */
  4442.     int bufsize = bp - ip->bufp;
  4443.     int extra = newlines;
  4444.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  4445.     int final_start = 0;
  4446.  
  4447.     bcopy (ip->bufp, buffer, bufsize);
  4448.     ip->bufp = bp;
  4449.     ip->lineno += newlines;
  4450.  
  4451.     while (bp == ip->buf + ip->length) {
  4452.       if (instack[indepth].macro == 0) {
  4453.     free (buffer);
  4454.     return "unterminated macro call";
  4455.       }
  4456.       ip->macro->type = T_MACRO;
  4457.       free (ip->buf);
  4458.       ip = &instack[--indepth];
  4459.       newlines = 0;
  4460.       comments = 0;
  4461.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  4462.             &newlines, &comments);
  4463.       final_start = bufsize;
  4464.       bufsize += bp - ip->bufp;
  4465.       extra += newlines;
  4466.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  4467.       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  4468.       ip->bufp = bp;
  4469.       ip->lineno += newlines;
  4470.     }
  4471.  
  4472.     /* Now, if arg is actually wanted, record its raw form,
  4473.        discarding comments and duplicating newlines in whatever
  4474.        part of it did not come from a macro expansion.
  4475.        EXTRA space has been preallocated for duplicating the newlines.
  4476.        FINAL_START is the index of the start of that part.  */
  4477.     if (argptr != 0) {
  4478.       argptr->raw = buffer;
  4479.       argptr->raw_length = bufsize;
  4480.       argptr->free1 = buffer;
  4481.       argptr->newlines = newlines;
  4482.       argptr->comments = comments;
  4483.       if ((newlines || comments) && ip->fname != 0)
  4484.     argptr->raw_length
  4485.       = final_start +
  4486.         discard_comments (argptr->raw + final_start,
  4487.                   argptr->raw_length - final_start,
  4488.                   newlines);
  4489.       argptr->raw[argptr->raw_length] = 0;
  4490.       if (argptr->raw_length > bufsize + extra)
  4491.     abort ();
  4492.     }
  4493.   }
  4494.  
  4495.   /* If we are not discarding this argument,
  4496.      macroexpand it and compute its length as stringified.
  4497.      All this info goes into *ARGPTR.  */
  4498.  
  4499.   if (argptr != 0) {
  4500.     FILE_BUF obuf;
  4501.     register U_CHAR *buf, *lim;
  4502.     register int totlen;
  4503.  
  4504.     obuf = expand_to_temp_buffer (argptr->raw,
  4505.                   argptr->raw + argptr->raw_length,
  4506.                   1);
  4507.  
  4508.     argptr->expanded = obuf.buf;
  4509.     argptr->expand_length = obuf.length;
  4510.     argptr->free2 = obuf.buf;
  4511.  
  4512.     buf = argptr->raw;
  4513.     lim = buf + argptr->raw_length;
  4514.  
  4515.     while (buf != lim && is_space[*buf])
  4516.       buf++;
  4517.     while (buf != lim && is_space[lim[-1]])
  4518.       lim--;
  4519.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  4520.     while (buf != lim) {
  4521.       register U_CHAR c = *buf++;
  4522.       totlen++;
  4523.       /* Internal sequences of whitespace are replaced by one space.  */
  4524.       if (is_space[c])
  4525.     SKIP_ALL_WHITE_SPACE (buf);
  4526.       else if (c == '\"' || c == '\\') /* escape these chars */
  4527.     totlen++;
  4528.       else if (!isprint (c))
  4529.     totlen += 3;
  4530.     }
  4531.     argptr->stringified_length = totlen;
  4532.   }
  4533.   return 0;
  4534. }
  4535.  
  4536. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  4537.    counting parens in *DEPTHPTR,
  4538.    and return if reach LIMIT
  4539.    or before a `)' that would make *DEPTHPTR negative
  4540.    or before a comma when *DEPTHPTR is zero.
  4541.    Single and double quotes are matched and termination
  4542.    is inhibited within them.  Comments also inhibit it.
  4543.    Value returned is pointer to stopping place.
  4544.  
  4545.    Increment *NEWLINES each time a newline is passed.
  4546.    Set *COMMENTS to 1 if a comment is seen.  */
  4547.  
  4548. U_CHAR *
  4549. macarg1 (start, limit, depthptr, newlines, comments)
  4550.      U_CHAR *start;
  4551.      register U_CHAR *limit;
  4552.      int *depthptr, *newlines, *comments;
  4553. {
  4554.   register U_CHAR *bp = start;
  4555.  
  4556.   while (bp < limit) {
  4557.     switch (*bp) {
  4558.     case '(':
  4559.       (*depthptr)++;
  4560.       break;
  4561.     case ')':
  4562.       if (--(*depthptr) < 0)
  4563.     return bp;
  4564.       break;
  4565.     case '\\':
  4566.       /* Backslash makes following char not special.  */
  4567.       if (bp + 1 < limit) bp++;
  4568.       break;
  4569.     case '\n':
  4570.       ++*newlines;
  4571.       break;
  4572.     case '/':
  4573.       if (bp[1] == '\\' && bp[2] == '\n')
  4574.     newline_fix (bp + 1);
  4575.       if (cplusplus && bp[1] == '/') {
  4576.     *comments = 1;
  4577.     bp += 2;
  4578.     while (bp < limit && *bp++ != '\n') ;
  4579.     ++*newlines;
  4580.     break;
  4581.       }
  4582.       if (bp[1] != '*' || bp + 1 >= limit)
  4583.     break;
  4584.       *comments = 1;
  4585.       bp += 2;
  4586.       while (bp + 1 < limit) {
  4587.     if (bp[0] == '*'
  4588.         && bp[1] == '\\' && bp[2] == '\n')
  4589.       newline_fix (bp + 1);
  4590.     if (bp[0] == '*' && bp[1] == '/')
  4591.       break;
  4592.     if (*bp == '\n') ++*newlines;
  4593.     bp++;
  4594.       }
  4595.       break;
  4596.     case '\'':
  4597.     case '\"':
  4598.       {
  4599.     int quotec;
  4600.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  4601.       if (*bp == '\\') {
  4602.         bp++;
  4603.         if (*bp == '\n')
  4604.           ++*newlines;
  4605.         while (*bp == '\\' && bp[1] == '\n') {
  4606.           bp += 2;
  4607.         }
  4608.       } else if (*bp == '\n') {
  4609.         ++*newlines;
  4610.         if (quotec == '\'')
  4611.           break;
  4612.       }
  4613.     }
  4614.       }
  4615.       break;
  4616.     case ',':
  4617.       if ((*depthptr) == 0)
  4618.     return bp;
  4619.       break;
  4620.     }
  4621.     bp++;
  4622.   }
  4623.  
  4624.   return bp;
  4625. }
  4626.  
  4627. /* Discard comments and duplicate newlines
  4628.    in the string of length LENGTH at START,
  4629.    except inside of string constants.
  4630.    The string is copied into itself with its beginning staying fixed.  
  4631.  
  4632.    NEWLINES is the number of newlines that must be duplicated.
  4633.    We assume that that much extra space is available past the end
  4634.    of the string.  */
  4635.  
  4636. int
  4637. discard_comments (start, length, newlines)
  4638.      U_CHAR *start;
  4639.      int length;
  4640.      int newlines;
  4641. {
  4642.   register U_CHAR *ibp;
  4643.   register U_CHAR *obp;
  4644.   register U_CHAR *limit;
  4645.   register int c;
  4646.  
  4647.   /* If we have newlines to duplicate, copy everything
  4648.      that many characters up.  Then, in the second part,
  4649.      we will have room to insert the newlines
  4650.      while copying down.
  4651.      NEWLINES may actually be too large, because it counts
  4652.      newlines in string constants, and we don't duplicate those.
  4653.      But that does no harm.  */
  4654.   if (newlines > 0) {
  4655.     ibp = start + length;
  4656.     obp = ibp + newlines;
  4657.     limit = start;
  4658.     while (limit != ibp)
  4659.       *--obp = *--ibp;
  4660.   }
  4661.  
  4662.   ibp = start + newlines;
  4663.   limit = start + length + newlines;
  4664.   obp = start;
  4665.  
  4666.   while (ibp < limit) {
  4667.     *obp++ = c = *ibp++;
  4668.     switch (c) {
  4669.     case '\n':
  4670.       /* Duplicate the newline.  */
  4671.       *obp++ = '\n';
  4672.       break;
  4673.  
  4674.     case '/':
  4675.       if (*ibp == '\\' && ibp[1] == '\n')
  4676.     newline_fix (ibp);
  4677.       /* Delete any comment.  */
  4678.       if (cplusplus && ibp[0] == '/') {
  4679.     obp--;
  4680.     ibp++;
  4681.     while (ibp < limit && *ibp++ != '\n') ;
  4682.     break;
  4683.       }
  4684.       if (ibp[0] != '*' || ibp + 1 >= limit)
  4685.     break;
  4686.       obp--;
  4687.       ibp++;
  4688.       while (ibp + 1 < limit) {
  4689.     if (ibp[0] == '*'
  4690.         && ibp[1] == '\\' && ibp[2] == '\n')
  4691.       newline_fix (ibp + 1);
  4692.     if (ibp[0] == '*' && ibp[1] == '/')
  4693.       break;
  4694.     ibp++;
  4695.       }
  4696.       ibp += 2;
  4697.       break;
  4698.  
  4699.     case '\'':
  4700.     case '\"':
  4701.       /* Notice and skip strings, so that we don't
  4702.      think that comments start inside them,
  4703.      and so we don't duplicate newlines in them.  */
  4704.       {
  4705.     int quotec = c;
  4706.     while (ibp < limit) {
  4707.       *obp++ = c = *ibp++;
  4708.       if (c == quotec)
  4709.         break;
  4710.       if (c == '\n' && quotec == '\'')
  4711.         break;
  4712.       if (c == '\\' && ibp < limit) {
  4713.         while (*ibp == '\\' && ibp[1] == '\n')
  4714.           ibp += 2;
  4715.         *obp++ = *ibp++;
  4716.       }
  4717.     }
  4718.       }
  4719.       break;
  4720.     }
  4721.   }
  4722.  
  4723.   return obp - start;
  4724. }
  4725.  
  4726. /*
  4727.  * error - print error message and increment count of errors.
  4728.  */
  4729. error (msg, arg1, arg2, arg3)
  4730.      char *msg;
  4731. {
  4732.   int i;
  4733.   FILE_BUF *ip = NULL;
  4734.  
  4735.   for (i = indepth; i >= 0; i--)
  4736.     if (instack[i].fname != NULL) {
  4737.       ip = &instack[i];
  4738.       break;
  4739.     }
  4740.  
  4741.   if (ip != NULL)
  4742.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4743.   fprintf (stderr, msg, arg1, arg2, arg3);
  4744.   fprintf (stderr, "\n");
  4745.   errors++;
  4746.   return 0;
  4747. }
  4748.  
  4749. /* Error including a message from `errno'.  */
  4750.  
  4751. error_from_errno (name)
  4752.      char *name;
  4753. {
  4754.   int i;
  4755.   FILE_BUF *ip = NULL;
  4756.   extern int errno, sys_nerr;
  4757.   extern char *sys_errlist[];
  4758.  
  4759.   for (i = indepth; i >= 0; i--)
  4760.     if (instack[i].fname != NULL) {
  4761.       ip = &instack[i];
  4762.       break;
  4763.     }
  4764.  
  4765.   if (ip != NULL)
  4766.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4767.  
  4768.   if (errno < sys_nerr)
  4769.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  4770.   else
  4771.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  4772.  
  4773.   errors++;
  4774.   return 0;
  4775. }
  4776.  
  4777. /* Print error message but don't count it.  */
  4778.  
  4779. warning (msg, arg1, arg2, arg3)
  4780.      char *msg;
  4781. {
  4782.   int i;
  4783.   FILE_BUF *ip = NULL;
  4784.  
  4785.   for (i = indepth; i >= 0; i--)
  4786.     if (instack[i].fname != NULL) {
  4787.       ip = &instack[i];
  4788.       break;
  4789.     }
  4790.  
  4791.   if (ip != NULL)
  4792.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4793.   fprintf (stderr, "warning: ");
  4794.   fprintf (stderr, msg, arg1, arg2, arg3);
  4795.   fprintf (stderr, "\n");
  4796.   return 0;
  4797. }
  4798.  
  4799. error_with_line (line, msg, arg1, arg2, arg3)
  4800.      int line;
  4801.      char *msg;
  4802. {
  4803.   int i;
  4804.   FILE_BUF *ip = NULL;
  4805.  
  4806.   for (i = indepth; i >= 0; i--)
  4807.     if (instack[i].fname != NULL) {
  4808.       ip = &instack[i];
  4809.       break;
  4810.     }
  4811.  
  4812.   if (ip != NULL)
  4813.     fprintf (stderr, "%s:%d: ", ip->fname, line);
  4814.   fprintf (stderr, msg, arg1, arg2, arg3);
  4815.   fprintf (stderr, "\n");
  4816.   errors++;
  4817.   return 0;
  4818. }
  4819.  
  4820. /* Return the line at which an error occurred.
  4821.    The error is not necessarily associated with the current spot
  4822.    in the input stack, so LINE says where.  LINE will have been
  4823.    copied from ip->lineno for the current input level.
  4824.    If the current level is for a file, we return LINE.
  4825.    But if the current level is not for a file, LINE is meaningless.
  4826.    In that case, we return the lineno of the innermost file.  */
  4827. int
  4828. line_for_error (line)
  4829.      int line;
  4830. {
  4831.   int i;
  4832.   int line1 = line;
  4833.  
  4834.   for (i = indepth; i >= 0; ) {
  4835.     if (instack[i].fname != 0)
  4836.       return line1;
  4837.     i--;
  4838.     if (i < 0)
  4839.       return 0;
  4840.     line1 = instack[i].lineno;
  4841.   }
  4842. }
  4843.  
  4844. /*
  4845.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  4846.  *
  4847.  * As things stand, nothing is ever placed in the output buffer to be
  4848.  * removed again except when it's KNOWN to be part of an identifier,
  4849.  * so flushing and moving down everything left, instead of expanding,
  4850.  * should work ok.
  4851.  */
  4852.  
  4853. int
  4854. grow_outbuf (obuf, needed)
  4855.      register FILE_BUF *obuf;
  4856.      register int needed;
  4857. {
  4858.   register U_CHAR *p;
  4859.   int minsize;
  4860.  
  4861.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  4862.     return;
  4863.  
  4864.   /* Make it at least twice as big as it is now.  */
  4865.   obuf->length *= 2;
  4866.   /* Make it have at least 150% of the free space we will need.  */
  4867.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  4868.   if (minsize > obuf->length)
  4869.     obuf->length = minsize;
  4870.  
  4871.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  4872.     memory_full ();
  4873.  
  4874.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  4875.   obuf->buf = p;
  4876. }
  4877.  
  4878. /* Symbol table for macro names and special symbols */
  4879.  
  4880. /*
  4881.  * install a name in the main hash table, even if it is already there.
  4882.  *   name stops with first non alphanumeric, except leading '#'.
  4883.  * caller must check against redefinition if that is desired.
  4884.  * delete_macro () removes things installed by install () in fifo order.
  4885.  * this is important because of the `defined' special symbol used
  4886.  * in #if, and also if pushdef/popdef directives are ever implemented.
  4887.  *
  4888.  * If LEN is >= 0, it is the length of the name.
  4889.  * Otherwise, compute the length by scanning the entire name.
  4890.  *
  4891.  * If HASH is >= 0, it is the precomputed hash code.
  4892.  * Otherwise, compute the hash code.
  4893.  */
  4894. HASHNODE *
  4895. install (name, len, type, value, hash)
  4896.      U_CHAR *name;
  4897.      int len;
  4898.      enum node_type type;
  4899.      int value;
  4900.      int hash;
  4901.         /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
  4902. {
  4903.   register HASHNODE *hp;
  4904.   register int i, bucket;
  4905.   register U_CHAR *p, *q;
  4906.  
  4907.   if (len < 0) {
  4908.     p = name;
  4909.     while (is_idchar[*p])
  4910.       p++;
  4911.     len = p - name;
  4912.   }
  4913.  
  4914.   if (hash < 0)
  4915.     hash = hashf (name, len, HASHSIZE);
  4916.  
  4917.   i = sizeof (HASHNODE) + len + 1;
  4918.   hp = (HASHNODE *) xmalloc (i);
  4919.   bucket = hash;
  4920.   hp->bucket_hdr = &hashtab[bucket];
  4921.   hp->next = hashtab[bucket];
  4922.   hashtab[bucket] = hp;
  4923.   hp->prev = NULL;
  4924.   if (hp->next != NULL)
  4925.     hp->next->prev = hp;
  4926.   hp->type = type;
  4927.   hp->length = len;
  4928.   hp->value.ival = value;
  4929.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  4930.   p = hp->name;
  4931.   q = name;
  4932.   for (i = 0; i < len; i++)
  4933.     *p++ = *q++;
  4934.   hp->name[len] = 0;
  4935.   return hp;
  4936. }
  4937.  
  4938. /*
  4939.  * find the most recent hash node for name name (ending with first
  4940.  * non-identifier char) installed by install
  4941.  *
  4942.  * If LEN is >= 0, it is the length of the name.
  4943.  * Otherwise, compute the length by scanning the entire name.
  4944.  *
  4945.  * If HASH is >= 0, it is the precomputed hash code.
  4946.  * Otherwise, compute the hash code.
  4947.  */
  4948. HASHNODE *
  4949. lookup (name, len, hash)
  4950.      U_CHAR *name;
  4951.      int len;
  4952.      int hash;
  4953. {
  4954.   register U_CHAR *bp;
  4955.   register HASHNODE *bucket;
  4956.  
  4957.   if (len < 0) {
  4958.     for (bp = name; is_idchar[*bp]; bp++) ;
  4959.     len = bp - name;
  4960.   }
  4961.  
  4962.   if (hash < 0)
  4963.     hash = hashf (name, len, HASHSIZE);
  4964.  
  4965.   bucket = hashtab[hash];
  4966.   while (bucket) {
  4967.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  4968.       return bucket;
  4969.     bucket = bucket->next;
  4970.   }
  4971.   return NULL;
  4972. }
  4973.  
  4974. /*
  4975.  * Delete a hash node.  Some weirdness to free junk from macros.
  4976.  * More such weirdness will have to be added if you define more hash
  4977.  * types that need it.
  4978.  */
  4979.  
  4980. /* Note that the DEFINITION of a macro is removed from the hash table
  4981.    but its storage is not freed.  This would be a storage leak
  4982.    except that it is not reasonable to keep undefining and redefining
  4983.    large numbers of macros many times.
  4984.    In any case, this is necessary, because a macro can be #undef'd
  4985.    in the middle of reading the arguments to a call to it.
  4986.    If #undef freed the DEFINITION, that would crash.  */
  4987.  
  4988. delete_macro (hp)
  4989.      HASHNODE *hp;
  4990. {
  4991.  
  4992.   if (hp->prev != NULL)
  4993.     hp->prev->next = hp->next;
  4994.   if (hp->next != NULL)
  4995.     hp->next->prev = hp->prev;
  4996.  
  4997.   /* make sure that the bucket chain header that
  4998.      the deleted guy was on points to the right thing afterwards. */
  4999.   if (hp == *hp->bucket_hdr)
  5000.     *hp->bucket_hdr = hp->next;
  5001.  
  5002. #if 0
  5003.   if (hp->type == T_MACRO) {
  5004.     DEFINITION *d = hp->value.defn;
  5005.     struct reflist *ap, *nextap;
  5006.  
  5007.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  5008.       nextap = ap->next;
  5009.       free (ap);
  5010.     }
  5011.     free (d);
  5012.   }
  5013. #endif
  5014.   free (hp);
  5015. }
  5016.  
  5017. /*
  5018.  * return hash function on name.  must be compatible with the one
  5019.  * computed a step at a time, elsewhere
  5020.  */
  5021. int
  5022. hashf (name, len, hashsize)
  5023.      register U_CHAR *name;
  5024.      register int len;
  5025.      int hashsize;
  5026. {
  5027.   register int r = 0;
  5028.  
  5029.   while (len--)
  5030.     r = HASHSTEP (r, *name++);
  5031.  
  5032.   return MAKE_POS (r) % hashsize;
  5033. }
  5034.  
  5035. /* Dump all macro definitions as #defines to stdout.  */
  5036.  
  5037. void
  5038. dump_all_macros ()
  5039. {
  5040.   int bucket;
  5041.  
  5042.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  5043.     register HASHNODE *hp;
  5044.  
  5045.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  5046.       if (hp->type == T_MACRO) {
  5047.     register DEFINITION *defn = hp->value.defn;
  5048.     struct reflist *ap;
  5049.     int offset;
  5050.     int concat;
  5051.  
  5052.  
  5053.     /* Print the definition of the macro HP.  */
  5054.  
  5055.     printf ("#define %s", hp->name);
  5056.     if (defn->nargs >= 0) {
  5057.       int i;
  5058.  
  5059.       printf ("(");
  5060.       for (i = 0; i < defn->nargs; i++) {
  5061.         dump_arg_n (defn, i);
  5062.         if (i + 1 < defn->nargs)
  5063.           printf (", ");
  5064.       }
  5065.       printf (")");
  5066.     }
  5067.  
  5068.     printf (" ");
  5069.  
  5070.     offset = 0;
  5071.     concat = 0;
  5072.     for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  5073.       dump_defn_1 (defn->expansion, offset, ap->nchars);
  5074.       if (ap->nchars != 0)
  5075.         concat = 0;
  5076.       offset += ap->nchars;
  5077.       if (ap->stringify)
  5078.         printf (" #");
  5079.       if (ap->raw_before && !concat)
  5080.         printf (" ## ");
  5081.       concat = 0;
  5082.       dump_arg_n (defn, ap->argno);
  5083.       if (ap->raw_after) {
  5084.         printf (" ## ");
  5085.         concat = 1;
  5086.       }
  5087.     }
  5088.     dump_defn_1 (defn->expansion, offset, defn->length - offset);
  5089.     printf ("\n");
  5090.       }
  5091.     }
  5092.   }
  5093. }
  5094.  
  5095. /* Output to stdout a substring of a macro definition.
  5096.    BASE is the beginning of the definition.
  5097.    Output characters START thru LENGTH.
  5098.    Discard newlines outside of strings, thus
  5099.    converting funny-space markers to ordinary spaces.  */
  5100.  
  5101. dump_defn_1 (base, start, length)
  5102.      U_CHAR *base;
  5103.      int start;
  5104.      int length;
  5105. {
  5106.   U_CHAR *p = base + start;
  5107.   U_CHAR *limit = base + start + length;
  5108.  
  5109.   while (p < limit) {
  5110.     if (*p != '\n')
  5111.       putchar (*p);
  5112.     else if (*p == '\"' || *p =='\'') {
  5113.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
  5114.       fwrite (p, p1 - p, 1, stdout);
  5115.       p = p1 - 1;
  5116.     }
  5117.     p++;
  5118.   }
  5119. }
  5120.  
  5121. /* Print the name of argument number ARGNUM of macro definition DEFN.
  5122.    Recall that DEFN->argnames contains all the arg names
  5123.    concatenated in reverse order with comma-space in between.  */
  5124.  
  5125. dump_arg_n (defn, argnum)
  5126.      DEFINITION *defn;
  5127.      int argnum;
  5128. {
  5129.   register U_CHAR *p = defn->argnames;
  5130.   while (argnum + 1 < defn->nargs) {
  5131.     p = (U_CHAR *) index (p, ' ') + 1;
  5132.     argnum++;
  5133.   }
  5134.  
  5135.   while (*p && *p != ',') {
  5136.     putchar (*p);
  5137.     p++;
  5138.   }
  5139. }
  5140.  
  5141. /* Initialize syntactic classifications of characters.  */
  5142.  
  5143. initialize_char_syntax ()
  5144. {
  5145.   register int i;
  5146.  
  5147.   /*
  5148.    * Set up is_idchar and is_idstart tables.  These should be
  5149.    * faster than saying (is_alpha (c) || c == '_'), etc.
  5150.    * Must do set up these things before calling any routines tthat
  5151.    * refer to them.
  5152.    */
  5153.   for (i = 'a'; i <= 'z'; i++) {
  5154.     is_idchar[i - 'a' + 'A'] = 1;
  5155.     is_idchar[i] = 1;
  5156.     is_idstart[i - 'a' + 'A'] = 1;
  5157.     is_idstart[i] = 1;
  5158.   }
  5159.   for (i = '0'; i <= '9'; i++)
  5160.     is_idchar[i] = 1;
  5161.   is_idchar['_'] = 1;
  5162.   is_idstart['_'] = 1;
  5163.   is_idchar['$'] = dollars_in_ident;
  5164.   is_idstart['$'] = dollars_in_ident;
  5165.  
  5166.   /* horizontal space table */
  5167.   is_hor_space[' '] = 1;
  5168.   is_hor_space['\t'] = 1;
  5169.   is_hor_space['\v'] = 1;
  5170.   is_hor_space['\f'] = 1;
  5171.  
  5172.   is_space[' '] = 1;
  5173.   is_space['\t'] = 1;
  5174.   is_space['\v'] = 1;
  5175.   is_space['\f'] = 1;
  5176.   is_space['\n'] = 1;
  5177. }
  5178.  
  5179. /* Initialize the built-in macros.  */
  5180.  
  5181. initialize_builtins ()
  5182. {
  5183.   install ("__LINE__", -1, T_SPECLINE, 0, -1);
  5184.   install ("__DATE__", -1, T_DATE, 0, -1);
  5185.   install ("__FILE__", -1, T_FILE, 0, -1);
  5186.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, -1);
  5187.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, -1);
  5188.   install ("__VERSION__", -1, T_VERSION, 0, -1);
  5189.   install ("__TIME__", -1, T_TIME, 0, -1);
  5190.   if (!traditional) {
  5191.     DEFINITION *defn;
  5192. #ifdef ds3100
  5193.     U_CHAR *bp = (unsigned char *)"1";
  5194. #else
  5195.     U_CHAR bp[] = "1";
  5196. #endif
  5197.  
  5198.     /* 
  5199.      * __STDC__ is a T_MACRO instead of a T_CONST so it
  5200.      * can be #undef'ed
  5201.      */
  5202.     defn = collect_expansion(bp, bp + 1, -1, 0);
  5203.     install ("__STDC__", -1, T_MACRO, defn, -1);
  5204.   }
  5205.  
  5206. /*  install ("__GNU__", -1, T_CONST, 1, -1);  */
  5207. /*  This is supplied using a -D by the compiler driver
  5208.     so that it is present only when truly compiling with GNU C.  */
  5209. }
  5210.  
  5211. /*
  5212.  * process a given definition string, for initialization
  5213.  * If STR is just an identifier, define it with value 1.
  5214.  * If STR has anything after the identifier, then it should
  5215.  * be identifier-space-definition.
  5216.  */
  5217. make_definition (str)
  5218.      U_CHAR *str;
  5219. {
  5220.   FILE_BUF *ip;
  5221.   struct directive *kt;
  5222.   U_CHAR *buf, *p;
  5223.  
  5224.   buf = str;
  5225.   p = str;
  5226.   while (is_idchar[*p]) p++;
  5227.   if (*p == 0) {
  5228.     buf = (U_CHAR *) alloca (p - buf + 4);
  5229.     strcpy ((char *)buf, str);
  5230.     strcat ((char *)buf, " 1");
  5231.   }
  5232.   
  5233.   ip = &instack[++indepth];
  5234.   ip->fname = "*Initialization*";
  5235.  
  5236.   ip->buf = ip->bufp = buf;
  5237.   ip->length = strlen (buf);
  5238.   ip->lineno = 1;
  5239.   ip->macro = 0;
  5240.   ip->free_ptr = 0;
  5241.   ip->if_stack = if_stack;
  5242.  
  5243.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  5244.     ;
  5245.  
  5246.   /* pass NULL as output ptr to do_define since we KNOW it never
  5247.      does any output.... */
  5248.   do_define (buf, buf + strlen (buf) , NULL, kt);
  5249.   --indepth;
  5250. }
  5251.  
  5252. /* JF, this does the work for the -U option */
  5253. make_undef (str)
  5254.      U_CHAR *str;
  5255. {
  5256.   FILE_BUF *ip;
  5257.   struct directive *kt;
  5258.  
  5259.   ip = &instack[++indepth];
  5260.   ip->fname = "*undef*";
  5261.  
  5262.   ip->buf = ip->bufp = str;
  5263.   ip->length = strlen (str);
  5264.   ip->lineno = 1;
  5265.   ip->macro = 0;
  5266.   ip->free_ptr = 0;
  5267.   ip->if_stack = if_stack;
  5268.  
  5269.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  5270.     ;
  5271.  
  5272.   do_undef (str,str + strlen (str) - 1, NULL, kt);
  5273.   --indepth;
  5274. }
  5275.  
  5276. /* Add output to `deps_buffer' for the -M switch.
  5277.    STRING points to the text to be output.
  5278.    SIZE is the number of bytes, or 0 meaning output until a null.
  5279.    If SIZE is nonzero, we break the line first, if it is long enough.  */
  5280.  
  5281. deps_output (string, size)
  5282.      char *string;
  5283.      int size;
  5284. {
  5285. #ifndef MAX_OUTPUT_COLUMNS
  5286. #define MAX_OUTPUT_COLUMNS 75
  5287. #endif
  5288.   if (size != 0 && deps_column != 0
  5289.       && size + deps_column > MAX_OUTPUT_COLUMNS) {
  5290.     deps_output ("\\\n  ", 0);
  5291.     deps_column = 0;
  5292.   }
  5293.  
  5294.   if (size == 0)
  5295.     size = strlen (string);
  5296.  
  5297.   if (deps_size + size + 1 > deps_allocated_size) {
  5298.     deps_allocated_size = deps_size + size + 50;
  5299.     deps_allocated_size *= 2;
  5300.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  5301.   }
  5302.   bcopy (string, &deps_buffer[deps_size], size);
  5303.   deps_size += size;
  5304.   deps_column += size;
  5305.   deps_buffer[deps_size] = 0;
  5306. }
  5307.  
  5308. #ifndef BSD
  5309. #ifndef BSTRING
  5310.  
  5311. void
  5312. bzero (b, length)
  5313.      register char *b;
  5314.      register int length;
  5315. {
  5316. #ifdef VMS
  5317.   short zero = 0;
  5318.   long max_str = 65535;
  5319.  
  5320.   while (length > max_str) {
  5321.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  5322.     length -= max_str;
  5323.     b += max_str;
  5324.   }
  5325.   (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  5326. #else
  5327.   while (length-- > 0)
  5328.     *b++ = 0;
  5329. #endif /* not VMS */
  5330. }
  5331.  
  5332. void
  5333. bcopy (b1, b2, length)
  5334.      register char *b1;
  5335.      register char *b2;
  5336.      register int length;
  5337. {
  5338. #ifdef VMS
  5339.   long max_str = 65535;
  5340.  
  5341.   while (length > max_str) {
  5342.     (void) LIB$MOVC3 (&max_str, b1, b2);
  5343.     length -= max_str;
  5344.     b1 += max_str;
  5345.     b2 += max_str;
  5346.   }
  5347.   (void) LIB$MOVC3 (&length, b1, b2);
  5348. #else
  5349.   while (length-- > 0)
  5350.     *b2++ = *b1++;
  5351. #endif /* not VMS */
  5352. }
  5353.  
  5354. int
  5355. bcmp (b1, b2, length)    /* This could be a macro! */
  5356.      register char *b1;
  5357.      register char *b2;
  5358.       register int length;
  5359. {
  5360. #ifdef VMS
  5361.    struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  5362.    struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  5363.  
  5364.    return STR$COMPARE (&src1, &src2);
  5365. #else
  5366.    while (length-- > 0)
  5367.      if (*b1++ != *b2++)
  5368.        return 1;
  5369.  
  5370.    return 0;
  5371. #endif /* not VMS */
  5372. }
  5373. #endif /* not BSTRING */
  5374. #endif /* not BSD */
  5375.  
  5376.  
  5377. void
  5378. fatal (str, arg)
  5379.      char *str, *arg;
  5380. {
  5381.   fprintf (stderr, "%s: ", progname);
  5382.   fprintf (stderr, str, arg);
  5383.   fprintf (stderr, "\n");
  5384.   exit (FATAL_EXIT_CODE);
  5385. }
  5386.  
  5387. /* More 'friendly' abort that prints the line and file.
  5388.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  5389.  
  5390. void
  5391. fancy_abort ()
  5392. {
  5393.   fatal ("Internal gcc abort.");
  5394. }
  5395.  
  5396. void
  5397. perror_with_name (name)
  5398.      char *name;
  5399. {
  5400.   extern int errno, sys_nerr;
  5401.   extern char *sys_errlist[];
  5402.  
  5403.   fprintf (stderr, "%s: ", progname);
  5404.   if (errno < sys_nerr)
  5405.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  5406.   else
  5407.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  5408.   errors++;
  5409. }
  5410.  
  5411. void
  5412. pfatal_with_name (name)
  5413.      char *name;
  5414. {
  5415.   perror_with_name (name);
  5416. #ifdef VMS
  5417.   exit (vaxc$errno);
  5418. #else
  5419.   exit (FATAL_EXIT_CODE);
  5420. #endif
  5421. }
  5422.  
  5423.  
  5424. void
  5425. memory_full ()
  5426. {
  5427.   fatal ("Memory exhausted.");
  5428. }
  5429.  
  5430.  
  5431. char *
  5432. xmalloc (size)
  5433.      int size;
  5434. {
  5435.   extern char *malloc ();
  5436.   register char *ptr = malloc (size);
  5437.   if (ptr != 0) return (ptr);
  5438.   memory_full ();
  5439.   /*NOTREACHED*/
  5440. }
  5441.  
  5442. char *
  5443. xrealloc (old, size)
  5444.      char *old;
  5445.      int size;
  5446. {
  5447.   extern char *realloc ();
  5448.   register char *ptr = realloc (old, size);
  5449.   if (ptr != 0) return (ptr);
  5450.   memory_full ();
  5451.   /*NOTREACHED*/
  5452. }
  5453.  
  5454. char *
  5455. xcalloc (number, size)
  5456.      int number, size;
  5457. {
  5458.   extern char *malloc ();
  5459.   register int total = number * size;
  5460.   register char *ptr = malloc (total);
  5461.   if (ptr != 0) {
  5462.     if (total > 100)
  5463.       bzero (ptr, total);
  5464.     else {
  5465.       /* It's not too long, so loop, zeroing by longs.
  5466.      It must be safe because malloc values are always well aligned.  */
  5467.       register long *zp = (long *) ptr;
  5468.       register long *zl = (long *) (ptr + total - 4);
  5469.       register int i = total - 4;
  5470.       while (zp < zl)
  5471.     *zp++ = 0;
  5472.       if (i < 0)
  5473.     i = 0;
  5474.       while (i < total)
  5475.     ptr[i++] = 0;
  5476.     }
  5477.     return ptr;
  5478.   }
  5479.   memory_full ();
  5480.   /*NOTREACHED*/
  5481. }
  5482.  
  5483. char *
  5484. savestring (input)
  5485.      char *input;
  5486. {
  5487.   int size = strlen (input);
  5488.   char *output = xmalloc (size + 1);
  5489.   strcpy (output, input);
  5490.   return output;
  5491. }
  5492.  
  5493. /* Get the file-mode and data size of the file open on FD
  5494.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  5495.  
  5496. int
  5497. file_size_and_mode (fd, mode_pointer, size_pointer)
  5498.      int fd;
  5499.      int *mode_pointer;
  5500.      long int *size_pointer;
  5501. {
  5502.   struct stat sbuf;
  5503.  
  5504.   if (fstat (fd, &sbuf) < 0) return (-1);
  5505.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  5506.   if (size_pointer) *size_pointer = sbuf.st_size;
  5507.   return 0;
  5508. }
  5509.  
  5510. #ifdef    VMS
  5511.  
  5512. /* Under VMS we need to fix up the "include" specification
  5513.    filename so that everything following the 1st slash is
  5514.    changed into its correct VMS file specification. */
  5515.  
  5516. hack_vms_include_specification (fname)
  5517.      char *fname;
  5518. {
  5519.   register char *cp, *cp1, *cp2;
  5520.   char Local[512];
  5521.   extern char *index (), *rindex ();
  5522.  
  5523.   /* Ignore leading "./"s */
  5524.   while (fname[0] == '.' && fname[1] == '/')
  5525.     strcpy (fname, fname+2);
  5526.   /* Look for the boundary between the VMS and UNIX filespecs */
  5527.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  5528.   if (cp == 0) cp == rindex (fname, '>'); /* ... Ditto            */
  5529.   if (cp == 0) cp == rindex (fname, ':'); /* Look for end of devspec. */
  5530.   if (cp) {
  5531.     cp++;
  5532.   } else {
  5533.     cp = index (fname, '/');    /* Look for the "/" */
  5534.   }
  5535.   /* See if we found that 1st slash */
  5536.   if (cp == 0) return;        /* Nothing to do!!! */
  5537.   if (*cp != '/') return;    /* Nothing to do!!! */
  5538.   /* Point to the UNIX filename part (which needs to be fixed!) */
  5539.   cp1 = cp+1;
  5540.   /* If the directory spec is not rooted, we can just copy
  5541.      the UNIX filename part and we are done */
  5542.   if (((cp - fname) > 2)
  5543.       && ((cp[-1] == ']') || (cp[-1] == '>'))
  5544.       && (cp[-2] != '.')) {
  5545.     strcpy (cp, cp1);
  5546.     return;
  5547.   }
  5548.   /* If there are no other slashes then the filename will be
  5549.      in the "root" directory.  Otherwise, we need to add
  5550.      directory specifications. */
  5551.   if (index (cp1, '/') == 0) {
  5552.     /* Just add "[000000]" as the directory string */
  5553.     strcpy (Local, "[000000]");
  5554.     cp2 = Local + strlen (Local);
  5555.   } else {
  5556.     /* Open the directory specification */
  5557.     cp2 = Local;
  5558.     *cp2++ = '[';
  5559.     /* As long as there are still subdirectories to add, do them. */
  5560.     while (index (cp1, '/') != 0) {
  5561.       /* If this token is "." we can ignore it */
  5562.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  5563.     cp1 += 2;
  5564.     continue;
  5565.       }
  5566.       /* Add a subdirectory spec. */
  5567.       if (cp2 != Local+1) *cp2++ = '.';
  5568.       /* If this is ".." then the spec becomes "-" */
  5569.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  5570.     /* Add "-" and skip the ".." */
  5571.     *cp2++ = '-';
  5572.     cp1 += 3;
  5573.     continue;
  5574.       }
  5575.       /* Copy the subdirectory */
  5576.       while (*cp1 != '/') *cp2++= *cp1++;
  5577.       cp1++;            /* Skip the "/" */
  5578.     }
  5579.     /* Close the directory specification */
  5580.     *cp2++ = ']';
  5581.   }
  5582.   /* Now add the filename */
  5583.   while (*cp1) *cp2++ = *cp1++;
  5584.   *cp2 = 0;
  5585.   /* Now append it to the original VMS spec. */
  5586.   strcpy (cp, Local);
  5587.   return;
  5588. }
  5589. #endif    /* VMS */
  5590.  
  5591. #ifdef    VMS
  5592.  
  5593. /* These are the read/write replacement routines for
  5594.    VAX-11 "C".  They make read/write behave enough
  5595.    like their UNIX counterparts that CCCP will work */
  5596.  
  5597. int
  5598. read (fd, buf, size)
  5599.      int fd;
  5600.      char *buf;
  5601.      int size;
  5602. {
  5603. #undef    read    /* Get back the REAL read routine */
  5604.   register int i;
  5605.   register int total = 0;
  5606.  
  5607.   /* Read until the buffer is exhausted */
  5608.   while (size > 0) {
  5609.     /* Limit each read to 32KB */
  5610.     i = (size > (32*1024)) ? (32*1024) : size;
  5611.     i = read (fd, buf, i);
  5612.     if (i <= 0) {
  5613.       if (i == 0) return (total);
  5614.       return(i);
  5615.     }
  5616.     /* Account for this read */
  5617.     total += i;
  5618.     buf += i;
  5619.     size -= i;
  5620.   }
  5621.   return (total);
  5622. }
  5623.  
  5624. int
  5625. write (fd, buf, size)
  5626.      int fd;
  5627.      char *buf;
  5628.      int size;
  5629. {
  5630. #undef    write    /* Get back the REAL write routine */
  5631.   int i;
  5632.   int j;
  5633.  
  5634.   /* Limit individual writes to 32Kb */
  5635.   i = size;
  5636.   while (i > 0) {
  5637.     j = (i > (32*1024)) ? (32*1024) : i;
  5638.     if (write (fd, buf, j) < 0) return (-1);
  5639.     /* Account for the data written */
  5640.     buf += j;
  5641.     i -= j;
  5642.   }
  5643.   return (size);
  5644. }
  5645.  
  5646. #endif /* VMS */
  5647.